From cfa668bd480649dde0cb513930c2eb4e0a89bb7e Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 22 Nov 2023 12:17:16 -0800 Subject: [PATCH] fix: Include manually added project refs in sync process. (#1201) * Add changes. * Add tests. * Update proto. * Bump. --- .yarn/versions/8f77371b.yml | 9 ++ Cargo.lock | 4 +- Cargo.toml | 2 +- .../typescript/platform/src/sync_project.rs | 120 ++++++++++-------- .../platform/tests/sync_project_test.rs | 75 +++++++++++ packages/cli/CHANGELOG.md | 13 ++ 6 files changed, 169 insertions(+), 54 deletions(-) create mode 100644 .yarn/versions/8f77371b.yml diff --git a/.yarn/versions/8f77371b.yml b/.yarn/versions/8f77371b.yml new file mode 100644 index 00000000000..1f2a5d9c201 --- /dev/null +++ b/.yarn/versions/8f77371b.yml @@ -0,0 +1,9 @@ +releases: + "@moonrepo/cli": patch + "@moonrepo/core-linux-arm64-gnu": patch + "@moonrepo/core-linux-arm64-musl": patch + "@moonrepo/core-linux-x64-gnu": patch + "@moonrepo/core-linux-x64-musl": patch + "@moonrepo/core-macos-arm64": patch + "@moonrepo/core-macos-x64": patch + "@moonrepo/core-windows-x64-msvc": patch diff --git a/Cargo.lock b/Cargo.lock index caaf1389661..58e44f4a9fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4655,9 +4655,9 @@ dependencies = [ [[package]] name = "proto_core" -version = "0.23.3" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f7dbc476e051d23cfac2b1f284ffa8943d45dffe40113dcdf113667b3dd8b79" +checksum = "37b4467e6e2819e74fc2247ff2d69486713414386fcc6549d172748bbcfa2a57" dependencies = [ "cached", "extism", diff --git a/Cargo.toml b/Cargo.toml index 124c78a1df1..ed91ea16424 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ pathdiff = "0.2.1" petgraph = { version = "0.6.4", default-features = false, features = [ "serde-1", ] } -proto_core = "0.23.3" +proto_core = "0.23.4" relative-path = { version = "1.9.0", features = ["serde"] } regex = "1.10.2" reqwest = { version = "0.11.22", default-features = false, features = [ diff --git a/crates/typescript/platform/src/sync_project.rs b/crates/typescript/platform/src/sync_project.rs index 7594cde8ad0..237dd9941ee 100644 --- a/crates/typescript/platform/src/sync_project.rs +++ b/crates/typescript/platform/src/sync_project.rs @@ -152,8 +152,10 @@ impl<'app> TypeScriptSyncer<'app> { &self.typescript_config.project_config_file_name, |tsconfig_json| { let mut mutated_tsconfig = false; + let should_include_sources = self.should_include_project_reference_sources(); + let should_sync_paths = self.should_sync_project_references_to_paths(); - // Include + // Add shared types to include if self.should_include_shared_types() && self.types_root.join("types").exists() && tsconfig_json.add_include_path(self.types_root.join("types/**/*"))? @@ -161,74 +163,90 @@ impl<'app> TypeScriptSyncer<'app> { mutated_tsconfig = true; } - // Project references + // Sync dependencies as project references if self.should_sync_project_references() && !tsconfig_project_refs.is_empty() { - let mut tsconfig_compiler_paths = BTreeMap::default(); - let should_include_sources = self.should_include_project_reference_sources(); - let should_sync_paths = self.should_sync_project_references_to_paths(); - for ref_path in tsconfig_project_refs { if tsconfig_json.add_project_ref( - &ref_path, + ref_path, &self.typescript_config.project_config_file_name, )? { mutated_tsconfig = true; } + } + } - // Include - if should_include_sources - && tsconfig_json.add_include_path(ref_path.join("**/*"))? - { - mutated_tsconfig = true; - } + // Map all project references (not just synced) to other fields + if should_include_sources || should_sync_paths { + if let Some(local_project_refs) = tsconfig_json.references.clone() { + let mut tsconfig_compiler_paths = BTreeMap::default(); + + for project_ref in local_project_refs { + let mut abs_ref = + path::normalize(self.project.root.join(&project_ref.path)); + + // Remove the tsconfig.json file name if it exists + if project_ref.path.ends_with(".json") { + abs_ref = abs_ref.parent().unwrap().to_path_buf(); + } - // Paths - if should_sync_paths { - if let Some(dep_package_json) = PackageJson::read(&ref_path)? { - if let Some(dep_package_name) = &dep_package_json.name { - for index in - ["src/index.ts", "src/index.tsx", "index.ts", "index.tsx"] - { - if ref_path.join(index).exists() { - tsconfig_compiler_paths.insert( - dep_package_name.clone(), - vec![to_relative_virtual_string( - ref_path.join(index), - &self.project.root, - )?], - ); - - break; + // include + if should_include_sources + && tsconfig_json.add_include_path(abs_ref.join("**/*"))? + { + mutated_tsconfig = true; + } + + // paths + if should_sync_paths { + if let Some(dep_package_json) = PackageJson::read(&abs_ref)? { + if let Some(dep_package_name) = &dep_package_json.name { + for index in [ + "src/index.ts", + "src/index.tsx", + "index.ts", + "index.tsx", + ] { + if abs_ref.join(index).exists() { + tsconfig_compiler_paths.insert( + dep_package_name.clone(), + vec![to_relative_virtual_string( + abs_ref.join(index), + &self.project.root, + )?], + ); + + break; + } } - } - tsconfig_compiler_paths.insert( - format!("{dep_package_name}/*"), - vec![to_relative_virtual_string( - ref_path.join(if ref_path.join("src").exists() { - "src/*" - } else { - "*" - }), - &self.project.root, - )?], - ); + tsconfig_compiler_paths.insert( + format!("{dep_package_name}/*"), + vec![to_relative_virtual_string( + abs_ref.join(if abs_ref.join("src").exists() { + "src/*" + } else { + "*" + }), + &self.project.root, + )?], + ); + } } } } - } - // Paths - if should_sync_paths - && tsconfig_json.update_compiler_options(|options| { - options.update_paths(tsconfig_compiler_paths) - }) - { - mutated_tsconfig = true; + // paths + if should_sync_paths + && tsconfig_json.update_compiler_options(|options| { + options.update_paths(tsconfig_compiler_paths) + }) + { + mutated_tsconfig = true; + } } } - // Out dir + // Route outDir to moon's cache if self.should_route_out_dir_to_cache() { let cache_route = get_cache_dir() .join("types") diff --git a/crates/typescript/platform/tests/sync_project_test.rs b/crates/typescript/platform/tests/sync_project_test.rs index bf5127a299c..46c9e21f555 100644 --- a/crates/typescript/platform/tests/sync_project_test.rs +++ b/crates/typescript/platform/tests/sync_project_test.rs @@ -602,6 +602,53 @@ mod sync_config { ); } + #[test] + fn includes_sources_from_manual_refs() { + let sandbox = create_sandbox("empty"); + sandbox.create_file("tsconfig.json", "{}"); + sandbox.create_file("packages/a/tsconfig.json", r#"{ "references": [{ "path": "../b/tsconfig.json" }, { "path": "../../common/c" }] }"#); + sandbox.create_file("packages/b/tsconfig.json", "{}"); + sandbox.create_file("common/c/tsconfig.json", "{}"); + + let project = Project { + id: Id::raw("project"), + root: sandbox.path().join("packages/a"), + ..Project::default() + }; + + let config = TypeScriptConfig { + include_project_reference_sources: true, + sync_project_references: true, + ..TypeScriptConfig::default() + }; + + TypeScriptSyncer::new(&project, &config, sandbox.path()) + .sync_project_tsconfig(FxHashSet::default()) + .unwrap(); + + let tsconfig = TsConfigJson::read_with_name(project.root, "tsconfig.json") + .unwrap() + .unwrap(); + + assert_eq!( + tsconfig.include.unwrap(), + vec!["../../common/c/**/*", "../b/**/*"] + ); + assert_eq!( + tsconfig.references.unwrap(), + vec![ + Reference { + path: "../b/tsconfig.json".into(), + prepend: None + }, + Reference { + path: "../../common/c".into(), + prepend: None + }, + ] + ); + } + #[test] fn doesnt_include_sources_when_sync_disabled() { let sandbox = create_sandbox("empty"); @@ -711,5 +758,33 @@ mod sync_config { ]) ); } + + #[test] + fn adds_from_manual_refs() { + let sandbox = create_sandbox("empty"); + sandbox.create_file("tsconfig.json", "{}"); + sandbox.create_file( + "packages/a/tsconfig.json", + r#"{ "references": [{ "path": "../d" }] }"#, + ); + sandbox.create_file("packages/a/package.json", r#"{ "name": "a" }"#); + sandbox.create_file("packages/b/package.json", r#"{ "name": "b" }"#); + sandbox.create_file("packages/b/src/file.ts", ""); // Not index on purpose + sandbox.create_file("common/c/package.json", r#"{ "name": "c" }"#); + sandbox.create_file("packages/d/package.json", r#"{ "name": "d" }"#); + sandbox.create_file("packages/d/index.ts", ""); + + let tsconfig = run_for_a(sandbox.path()); + + assert_eq!( + tsconfig.compiler_options.unwrap().paths.unwrap(), + BTreeMap::from_iter([ + ("b/*".into(), vec!["../b/src/*".into()]), + ("c/*".into(), vec!["../../common/c/*".into()]), + ("d".into(), vec!["../d/index.ts".into()]), + ("d/*".into(), vec!["../d/*".into()]), + ]) + ); + } } } diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 7d0cf3101fc..a29d8953951 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -10,6 +10,19 @@ - More accurately monitors signals (ctrl+c) and shutdowns. - Tasks can now be configured with a timeout. +## Unreleased + +#### 🐞 Fixes + +- Fixed an issue where executing moon (and indirectly proto) would run into privilege access issues + on Windows. +- Fixed `typescript.includeProjectReferenceSources` and `typescript.syncProjectReferencesToPaths` + settings not including project references that were manually added (not auto-synced). + +#### ⚙️ Internal + +- Updated proto to v0.23.4. + ## 1.17.3 #### 🐞 Fixes