Skip to content

Commit

Permalink
fix: Include manually added project refs in sync process. (#1201)
Browse files Browse the repository at this point in the history
* Add changes.

* Add tests.

* Update proto.

* Bump.
  • Loading branch information
milesj authored Nov 22, 2023
1 parent 2c5688e commit cfa668b
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 54 deletions.
9 changes: 9 additions & 0 deletions .yarn/versions/8f77371b.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
120 changes: 69 additions & 51 deletions crates/typescript/platform/src/sync_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,83 +152,101 @@ 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/**/*"))?
{
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")
Expand Down
75 changes: 75 additions & 0 deletions crates/typescript/platform/tests/sync_project_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()]),
])
);
}
}
}
13 changes: 13 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cfa668b

Please sign in to comment.