Skip to content

Commit a25d58c

Browse files
committed
feat(core): update additional project directories handling
Refine workspace file handling and path utilities to properly support additional project directories throughout the native and TypeScript layers.
1 parent a26bfc3 commit a25d58c

22 files changed

+255
-121
lines changed

packages/devkit/src/utils/calculate-hash-for-create-nodes.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ export async function calculateHashForCreateNodes(
1414
additionalGlobs: string[] = []
1515
): Promise<string> {
1616
return hashArray([
17-
await hashWithWorkspaceContext(
18-
context.workspaceRoot,
19-
context.nxJsonConfiguration.additionalProjectDirectories ?? [],
20-
[join(projectRoot, '**/*'), ...additionalGlobs]
21-
),
17+
await hashWithWorkspaceContext(context.workspaceRoot, [
18+
join(projectRoot, '**/*'),
19+
...additionalGlobs,
20+
]),
2221
hashObject(options),
2322
]);
2423
}

packages/devkit/src/utils/replace-project-configuration-with-plugin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import {
99
updateNxJson,
1010
updateProjectConfiguration,
1111
} from 'nx/src/devkit-exports';
12-
import { findProjectForPath, hashObject } from 'nx/src/devkit-internals';
13-
import { multiGlobInAdditionalProjectDirectories } from 'nx/src/native';
12+
import {
13+
findProjectForPath,
14+
hashObject,
15+
multiGlobInAdditionalProjectDirectories,
16+
} from 'nx/src/devkit-internals';
1417

1518
export async function replaceProjectConfigurationsWithPlugin<T = unknown>(
1619
tree: Tree,

packages/nx/bin/nx.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { performance } from 'perf_hooks';
2121
import { setupWorkspaceContext } from '../src/utils/workspace-context';
2222
import { daemonClient } from '../src/daemon/client/client';
2323
import { removeDbConnections } from '../src/utils/db-connection';
24-
import { readNxJson } from '../src/config/nx-json';
2524

2625
async function main() {
2726
if (

packages/nx/src/daemon/server/server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ import {
142142
REGISTER_PROJECT_GRAPH_LISTENER,
143143
} from '../message-types/register-project-graph-listener';
144144
import { deserialize, serialize } from 'v8';
145-
import { readNxJson } from '../../config/nx-json';
146145

147146
let performanceObserver: PerformanceObserver | undefined;
148147
let workspaceWatcherError: Error | undefined;

packages/nx/src/devkit-internals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export {
3535
createProjectRootMappingsFromProjectConfigurations,
3636
findProjectForPath,
3737
} from './project-graph/utils/find-project-for-path';
38+
export { multiGlobInAdditionalProjectDirectories } from './native';
3839
export { retrieveProjectConfigurations } from './project-graph/utils/retrieve-workspace-files';
3940
export { LoadedNxPlugin } from './project-graph/plugins/loaded-nx-plugin';
4041
export * from './project-graph/error-types';

packages/nx/src/hasher/hash-plan-inspector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class HashPlanInspector {
4444
false
4545
);
4646
this.inspector = new NativeHashPlanInspector(
47+
workspaceRoot,
4748
externalReferences.allWorkspaceFiles,
4849
this.projectGraphRef,
4950
externalReferences.projectFiles

packages/nx/src/native/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export declare class FileLock {
4343
}
4444

4545
export declare class HashPlanInspector {
46-
constructor(allWorkspaceFiles: ExternalObject<Array<FileData>>, projectGraph: ExternalObject<ProjectGraph>, projectFileMap: ExternalObject<Record<string, Array<FileData>>>)
46+
constructor(workspaceRoot: string, allWorkspaceFiles: ExternalObject<Array<FileData>>, projectGraph: ExternalObject<ProjectGraph>, projectFileMap: ExternalObject<Record<string, Array<FileData>>>)
4747
inspect(hashPlans: ExternalObject<Record<string, Array<HashInstruction>>>): Record<string, string[]>
4848
}
4949

@@ -136,7 +136,7 @@ export declare class Watcher {
136136
export declare class WorkspaceContext {
137137
workspaceRoot: string
138138
constructor(workspaceRoot: string, cacheDir: string)
139-
getWorkspaceFiles(projectRootMap: Record<string, string>): NxWorkspaceFiles
139+
getWorkspaceFiles(additionalProjectDirectories: Array<string>, projectRootMap: Record<string, string>): NxWorkspaceFiles
140140
glob(globs: Array<string>, exclude?: Array<string> | undefined | null): Array<string>
141141
/**
142142
* Performs multiple glob pattern matches against workspace files in parallel

packages/nx/src/native/tasks/hash_plan_inspector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::collections::HashMap;
99

1010
#[napi]
1111
pub struct HashPlanInspector {
12+
workspace_root: String,
1213
all_workspace_files: External<Vec<FileData>>,
1314
project_graph: External<ProjectGraph>,
1415
project_file_map: External<HashMap<String, Vec<FileData>>>,
@@ -18,11 +19,13 @@ pub struct HashPlanInspector {
1819
impl HashPlanInspector {
1920
#[napi(constructor)]
2021
pub fn new(
22+
workspace_root: String,
2123
all_workspace_files: External<Vec<FileData>>,
2224
project_graph: External<ProjectGraph>,
2325
project_file_map: External<HashMap<String, Vec<FileData>>>,
2426
) -> Self {
2527
Self {
28+
workspace_root,
2629
all_workspace_files,
2730
project_graph,
2831
project_file_map,
@@ -58,6 +61,7 @@ impl HashPlanInspector {
5861
.ok_or_else(|| anyhow!("project {} not found", project_name))?;
5962

6063
let files = collect_project_files(
64+
&self.workspace_root,
6165
project_name,
6266
&project.root,
6367
file_sets,

packages/nx/src/native/tasks/hashers/hash_project_files.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
use std::collections::HashMap;
2-
31
use anyhow::*;
2+
use std::collections::HashMap;
3+
use std::path::PathBuf;
44
use tracing::{trace, trace_span};
55

66
use crate::native::glob::build_glob_set;
77
use crate::native::types::FileData;
8+
use crate::native::utils::path::get_relative_path;
89

910
pub fn hash_project_files(
11+
workspace_root: &String,
1012
project_name: &str,
1113
project_root: &str,
1214
file_sets: &[String],
1315
project_file_map: &HashMap<String, Vec<FileData>>,
1416
) -> Result<String> {
1517
let _span = trace_span!("hash_project_files", project_name).entered();
16-
let collected_files =
17-
collect_project_files(project_name, project_root, file_sets, project_file_map)?;
18+
let collected_files = collect_project_files(
19+
workspace_root,
20+
project_name,
21+
project_root,
22+
file_sets,
23+
project_file_map,
24+
)?;
1825
trace!("collected_files: {:?}", collected_files.len());
1926
let mut hasher = xxhash_rust::xxh3::Xxh3::new();
2027
for file in collected_files {
@@ -26,6 +33,7 @@ pub fn hash_project_files(
2633

2734
/// base function that should be testable (to make sure that we're getting the proper files back)
2835
pub fn collect_project_files<'a>(
36+
workspace_root: &String,
2937
project_name: &str,
3038
project_root: &str,
3139
file_sets: &[String],
@@ -52,7 +60,15 @@ pub fn collect_project_files<'a>(
5260
let now = std::time::Instant::now();
5361
let hashes = files
5462
.iter()
55-
.filter(|file| glob_set.is_match(&file.file))
63+
.filter(|file| {
64+
let path = if PathBuf::from(&file.file).is_absolute() {
65+
&get_relative_path(workspace_root, &file.file)
66+
} else {
67+
&file.file
68+
};
69+
trace!("{} - {}", path, file.hash);
70+
glob_set.is_match(path)
71+
})
5672
.collect::<Vec<_>>();
5773
trace!("hash_files for {}: {:?}", project_name, now.elapsed());
5874
Ok(hashes)
@@ -68,6 +84,7 @@ mod tests {
6884

6985
#[test]
7086
fn test_collect_files() {
87+
let workspace_root = String::from("");
7188
let proj_name = "test_project";
7289
let proj_root = "test/root";
7390
let file_sets = &[
@@ -101,11 +118,14 @@ mod tests {
101118
],
102119
);
103120

104-
let result = collect_project_files(proj_name, proj_root, file_sets, &file_map).unwrap();
121+
let result =
122+
collect_project_files(&workspace_root, proj_name, proj_root, file_sets, &file_map)
123+
.unwrap();
105124

106125
assert_eq!(result, vec![&tsfile_1, &tsfile_2]);
107126

108127
let result = collect_project_files(
128+
&workspace_root,
109129
proj_name,
110130
proj_root,
111131
&["!{projectRoot}/**/*.spec.ts".into()],
@@ -124,6 +144,7 @@ mod tests {
124144

125145
#[test]
126146
fn should_hash_deterministically() {
147+
let workspace_root = String::from("");
127148
let proj_name = "test_project";
128149
let proj_root = "test/root";
129150
let file_sets = &[
@@ -156,7 +177,9 @@ mod tests {
156177
file_data4.clone(),
157178
],
158179
);
159-
let hash_result = hash_project_files(proj_name, proj_root, file_sets, &file_map).unwrap();
180+
let hash_result =
181+
hash_project_files(&workspace_root, proj_name, proj_root, file_sets, &file_map)
182+
.unwrap();
160183
assert_eq!(
161184
hash_result,
162185
hash(
@@ -173,6 +196,7 @@ mod tests {
173196

174197
#[test]
175198
fn should_hash_projects_with_root_as_dot() {
199+
let workspace_root = String::from("");
176200
let proj_name = "test_project";
177201
// having "." as the project root means that this would be a standalone project
178202
let proj_root = ".";
@@ -206,7 +230,9 @@ mod tests {
206230
file_data4.clone(),
207231
],
208232
);
209-
let hash_result = hash_project_files(proj_name, proj_root, file_sets, &file_map).unwrap();
233+
let hash_result =
234+
hash_project_files(&workspace_root, proj_name, proj_root, file_sets, &file_map)
235+
.unwrap();
210236
assert_eq!(
211237
hash_result,
212238
hash(

packages/nx/src/native/tasks/task_hasher.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl TaskHasher {
220220
.get(project_name)
221221
.ok_or_else(|| anyhow!("project {} not found", project_name))?;
222222
let hashed_project_files = hash_project_files(
223+
&self.workspace_root,
223224
project_name,
224225
&project.root,
225226
file_sets,

0 commit comments

Comments
 (0)