Skip to content

Commit

Permalink
Add package edition specification
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Nov 6, 2023
1 parent 44e8271 commit 28d74ca
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 16 deletions.
26 changes: 24 additions & 2 deletions scarb/src/compiler/db.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use anyhow::{anyhow, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::project::{ProjectConfig, ProjectConfigContent};
use cairo_lang_compiler::project::{
AllCratesConfig, ProjectConfig, ProjectConfigContent, SingleCrateConfig,
};
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::ModuleId;
use cairo_lang_defs::plugin::MacroPlugin;
use cairo_lang_filesystem::db::{AsFilesGroupMut, FilesGroup, FilesGroupEx};
use cairo_lang_filesystem::ids::{CrateLongId, Directory};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use smol_str::SmolStr;
use std::sync::Arc;
use tracing::trace;

Expand Down Expand Up @@ -87,13 +91,31 @@ fn build_project_config(unit: &CompilationUnit) -> Result<ProjectConfig> {
})
.collect();

let crates_config: OrderedHashMap<SmolStr, SingleCrateConfig> = unit
.components
.iter()
.map(|component| {
(
component.cairo_package_name(),
SingleCrateConfig {
edition: component.package.manifest.edition,
},
)
})
.collect();

let crates_config = AllCratesConfig {
override_map: crates_config,
..Default::default()
};

let corelib = Some(Directory::Real(
unit.core_package_component().target.source_root().into(),
));

let content = ProjectConfigContent {
crate_roots,
crates_config: Default::default(),
crates_config,
};

let project_config = ProjectConfig {
Expand Down
3 changes: 3 additions & 0 deletions scarb/src/core/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::{BTreeMap, HashSet};

use anyhow::{bail, ensure, Result};
use cairo_lang_filesystem::db::Edition;
use camino::Utf8PathBuf;
use derive_builder::Builder;
use semver::VersionReq;
Expand Down Expand Up @@ -41,6 +42,8 @@ pub struct Manifest {
pub summary: Summary,
pub targets: Vec<Target>,
#[builder(default)]
pub edition: Edition,
#[builder(default)]
pub metadata: ManifestMetadata,
#[builder(default = "ManifestCompilerConfig::default_for_profile(&Profile::DEV)")]
pub compiler_config: ManifestCompilerConfig,
Expand Down
12 changes: 12 additions & 0 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::default::Default;
use std::fs;

use anyhow::{anyhow, bail, ensure, Context, Result};
use cairo_lang_filesystem::db::Edition;
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;
use pathdiff::diff_utf8_paths;
Expand Down Expand Up @@ -103,6 +104,7 @@ pub struct TomlWorkspace {
#[serde(rename_all = "kebab-case")]
pub struct PackageInheritableFields {
pub version: Option<Version>,
pub edition: Option<Edition>,
pub authors: Option<Vec<String>>,
pub description: Option<String>,
pub documentation: Option<String>,
Expand Down Expand Up @@ -140,6 +142,7 @@ impl PackageInheritableFields {
get_field!(license, String);
get_field!(license_file, String);
get_field!(repository, String);
get_field!(edition, Edition);

pub fn readme(&self, workspace_root: &Utf8Path, package_root: &Utf8Path) -> Result<PathOrBool> {
let Ok(Some(readme)) = readme_for_package(workspace_root, self.readme.as_ref()) else {
Expand Down Expand Up @@ -177,6 +180,7 @@ type MaybeWorkspaceField<T> = MaybeWorkspace<T, TomlWorkspaceField>;
pub struct TomlPackage {
pub name: PackageName,
pub version: MaybeWorkspaceField<Version>,
pub edition: Option<MaybeWorkspaceField<Edition>>,
pub authors: Option<MaybeWorkspaceField<Vec<String>>>,
pub urls: Option<BTreeMap<String, String>>,
pub description: Option<MaybeWorkspaceField<String>>,
Expand Down Expand Up @@ -521,9 +525,17 @@ impl TomlManifest {
.transpose()?,
};

let edition = package
.edition
.clone()
.map(|edition| edition.resolve("edition", || inheritable_package.edition()))
.transpose()?
.unwrap_or_default();

let manifest = ManifestBuilder::default()
.summary(summary)
.targets(targets)
.edition(edition)
.metadata(metadata)
.compiler_config(compiler_config)
.scripts(scripts)
Expand Down
1 change: 1 addition & 0 deletions scarb/src/core/publishing/manifest_normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn generate_package(pkg: &Package) -> Box<TomlPackage> {
Box::new(TomlPackage {
name: summary.package_id.name.clone(),
version: MaybeWorkspace::Defined(summary.package_id.version.clone()),
edition: Some(MaybeWorkspace::Defined(pkg.manifest.edition)),
authors: metadata.authors.clone().map(MaybeWorkspace::Defined),
urls: metadata.urls.clone(),
description: metadata.description.clone().map(MaybeWorkspace::Defined),
Expand Down
52 changes: 52 additions & 0 deletions scarb/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,55 @@ fn workspace_as_dep() {
)),
);
}

#[test]
fn can_define_edition() {
let code = indoc! {r#"
fn example() -> Nullable<felt252> { null() }
"#};
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.lib_cairo(code)
.edition("2023_01")
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.success();

let t = TempDir::new().unwrap();
ProjectBuilder::start()
.lib_cairo(code)
.edition("2023_10")
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.failure();
}

#[test]
fn edition_must_exist() {
let t = TempDir::new().unwrap();
ProjectBuilder::start().edition("2021").build(&t);

Scarb::quick_snapbox()
.arg("fetch")
.current_dir(&t)
.assert()
.failure()
.stdout_matches(indoc! {r#"
error: failed to parse manifest at: /[..]/Scarb.toml
Caused by:
TOML parse error at line 4, column 11
|
4 | edition = "2021"
| ^^^^^^
unknown variant `2021`, expected `2023_01` or `2023_10`
"#});
}
10 changes: 5 additions & 5 deletions scarb/tests/local_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn publish() {
{
"v": "1.0.0",
"deps": [],
"cksum": "sha256:13973a8c7a6d86430ad569fd2c2d5cad282ba67ee587820a4b597f7b0a66a8dd",
"cksum": "sha256:d891504afc86fc0a7a9f38533a66ef2763990a1ff4be3eb9d5836d32a9bd9ad3",
}
])
);
Expand All @@ -192,12 +192,12 @@ fn publish() {
{
"v": "1.0.0",
"deps": [],
"cksum": "sha256:032b626571a86bb18d93d6e67376d5c9b5a14efd76871bb5e3de4b1ded3c6c64",
"cksum": "sha256:d05d4c524aa0136e42df6138f8e97f8b2b7fc946911cef8ae40baf38acf87ef6",
},
{
"v": "1.1.0",
"deps": [],
"cksum": "sha256:0b9c792212d383b00b3b059461caa1bea64b1528890d54f95ea678d2956ec613",
"cksum": "sha256:ec55410dac39c63ea1372f44f05b74bcf14ec6305749d80bd607be0603271ef1",
}
])
);
Expand Down Expand Up @@ -230,7 +230,7 @@ fn publish_overwrites_existing() {
{
"v": "1.0.0",
"deps": [],
"cksum": "sha256:d3356ff99d397d9963f88318b4c0019b61037255a9a632cc1fe24b9aa876a607",
"cksum": "sha256:49bb7566594c89da4603578aebe812d750d1fefa1fccc532461963d813093b11",
}
])
);
Expand Down Expand Up @@ -258,7 +258,7 @@ fn publish_overwrites_existing() {
{
"v": "1.0.0",
"deps": [],
"cksum": "sha256:207317e685713fcda79fa2172b5d3ca8d138efc7cee3c6c0960a17ba980738bd",
"cksum": "sha256:f6555b5b27327d40196578005de811158a3ac7401c36c13ee02b27afe7aab00f",
}
])
);
Expand Down
18 changes: 9 additions & 9 deletions scarb/tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ fn infer_readme_workspace() {
[package]
name = "t7"
version.workspace = true
edition = "2021"
edition = "2023_10"
readme.workspace = true
"#,
)
Expand All @@ -956,7 +956,7 @@ fn infer_readme_workspace() {
[workspace.package]
version = "0.1.0"
edition = "2021"
edition = "2023_10"
readme = "MEREAD.md"
[package]
Expand All @@ -974,7 +974,7 @@ fn infer_readme_workspace() {
[package]
name = "t1"
version.workspace = true
edition = "2021"
edition = "2023_10"
readme.workspace = true
"#,
)
Expand All @@ -986,7 +986,7 @@ fn infer_readme_workspace() {
[package]
name = "t2"
version.workspace = true
edition = "2021"
edition = "2023_10"
readme = true
"#,
)
Expand All @@ -998,7 +998,7 @@ fn infer_readme_workspace() {
[package]
name = "t3"
version.workspace = true
edition = "2021"
edition = "2023_10"
"#,
)
.unwrap();
Expand All @@ -1009,7 +1009,7 @@ fn infer_readme_workspace() {
[package]
name = "t4"
version.workspace = true
edition = "2021"
edition = "2023_10"
readme = "TEST.txt"
"#,
)
Expand All @@ -1021,7 +1021,7 @@ fn infer_readme_workspace() {
[package]
name = "t5"
version.workspace = true
edition = "2021"
edition = "2023_10"
readme = false
"#,
)
Expand All @@ -1033,7 +1033,7 @@ fn infer_readme_workspace() {
[package]
name = "t6"
version.workspace = true
edition = "2021"
edition = "2023_10"
"#,
)
.unwrap();
Expand All @@ -1046,7 +1046,7 @@ fn infer_readme_workspace() {
[package]
name = "t7"
version.workspace = true
edition = "2021"
edition = "2023_10"
readme.workspace = true
"#,
)
Expand Down
3 changes: 3 additions & 0 deletions scarb/tests/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ fn simple() {
[package]
name = "foo"
version = "1.0.0"
edition = "2023_01"
[dependencies]
"#},
Expand Down Expand Up @@ -356,6 +357,7 @@ fn generated_manifest() {
[package]
name = "hello"
version = "1.0.0"
edition = "2023_01"
[dependencies.git_dep]
version = "^0.2.0"
Expand Down Expand Up @@ -454,6 +456,7 @@ fn workspace() {
[package]
name = "hello"
version = "1.0.0"
edition = "2023_01"
[dependencies.path_dep]
version = "^1.0.0"
Expand Down
10 changes: 10 additions & 0 deletions utils/scarb-test-support/src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod to_version;
pub struct ProjectBuilder {
name: String,
version: Version,
edition: Option<String>,
cairo_version: Option<Version>,
src: HashMap<Utf8PathBuf, String>,
deps: Vec<(String, Value)>,
Expand All @@ -34,6 +35,7 @@ impl ProjectBuilder {
Self {
name: format!("pkg{n}"),
version: Version::new(1, n, 0),
edition: None,
cairo_version: None,
src: HashMap::from_iter([(
Utf8PathBuf::from("src/lib.cairo"),
Expand All @@ -54,6 +56,11 @@ impl ProjectBuilder {
self
}

pub fn edition(mut self, edition: impl ToString) -> Self {
self.edition = Some(edition.to_string());
self
}

pub fn cairo_version(mut self, cairo_version: impl ToVersion) -> Self {
self.cairo_version = Some(cairo_version.to_version().unwrap());
self
Expand Down Expand Up @@ -91,6 +98,9 @@ impl ProjectBuilder {
doc["package"] = toml_edit::table();
doc["package"]["name"] = Item::Value(Value::from(self.name.clone()));
doc["package"]["version"] = Item::Value(Value::from(self.version.to_string()));
if let Some(edition) = self.edition.as_ref() {
doc["package"]["edition"] = Item::Value(Value::from(edition.to_string()));
}
if let Some(cairo_version) = self.cairo_version.as_ref() {
doc["package"]["cairo-version"] = Item::Value(Value::from(cairo_version.to_string()));
}
Expand Down

0 comments on commit 28d74ca

Please sign in to comment.