Skip to content

Commit

Permalink
Merge branch 'master' into feat/interpolate-workspaceroot-changelog-r…
Browse files Browse the repository at this point in the history
…enderer
  • Loading branch information
fahslaj authored Feb 29, 2024
2 parents dd0bb02 + c523d52 commit 8f5f146
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ exports[`app generated files content - as-provided general application should co
"$schema": "../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"sourceRoot": "my-app/src",
"// targets": "to see all targets run: nx show project my-app --web",
"targets": {}
}
"
Expand Down
18 changes: 1 addition & 17 deletions packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { toProjectName, Workspaces } from './workspaces';
import { toProjectName } from './workspaces';
import { TempFs } from '../internal-testing-utils/temp-fs';
import { withEnvironmentVariables } from '../internal-testing-utils/with-environment';
import { retrieveProjectConfigurations } from '../project-graph/utils/retrieve-workspace-files';
import { readNxJson } from './configuration';
import { shutdownPluginWorkers } from '../project-graph/plugins/plugin-pool';

const libConfig = (root, name?: string) => ({
name: name ?? toProjectName(`${root}/some-file`),
projectType: 'library',
root: `libs/${root}`,
sourceRoot: `libs/${root}/src`,
targets: {
'nx-release-publish': {
dependsOn: ['^nx-release-publish'],
executor: '@nx/js:release-publish',
options: {},
},
},
});

describe('Workspaces', () => {
let fs: TempFs;
beforeEach(() => {
Expand Down Expand Up @@ -53,7 +39,6 @@ describe('Workspaces', () => {
},
() => retrieveProjectConfigurations(fs.tempDir, readNxJson(fs.tempDir))
);
await shutdownPluginWorkers();
expect(projects['my-package']).toEqual({
name: 'my-package',
root: 'packages/my-package',
Expand All @@ -67,7 +52,6 @@ describe('Workspaces', () => {
},
},
});
await shutdownPluginWorkers();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ async function processFilesAndCreateAndSerializeProjectGraph(): Promise<Serializ
serverLogger.requestLog([...updatedFiles.values()]);
serverLogger.requestLog([...deletedFiles]);
const nxJson = readNxJson(workspaceRoot);
global.NX_GRAPH_CREATION = true;
const graphNodes = await retrieveProjectConfigurations(
workspaceRoot,
nxJson
Expand All @@ -226,7 +227,9 @@ async function processFilesAndCreateAndSerializeProjectGraph(): Promise<Serializ
updatedFileHashes,
deletedFiles
);
return createAndSerializeProjectGraph(graphNodes);
const g = createAndSerializeProjectGraph(graphNodes);
delete global.NX_GRAPH_CREATION;
return g;
} catch (err) {
return Promise.resolve({
error: err,
Expand Down
77 changes: 74 additions & 3 deletions packages/nx/src/generators/utils/project-configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const projectConfiguration: ProjectConfiguration = {
name: 'test',
root: 'libs/test',
sourceRoot: 'libs/test/src',
targets: {},
};

describe('project configuration', () => {
Expand All @@ -32,20 +31,92 @@ describe('project configuration', () => {
});

it('should create project.json file when adding a project if standalone is true', () => {
addProjectConfiguration(tree, 'test', projectConfiguration);
addProjectConfiguration(tree, 'test', {
...projectConfiguration,
targets: {
test: {},
},
});

expect(readProjectConfiguration(tree, 'test')).toMatchInlineSnapshot(`
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"name": "test",
"root": "libs/test",
"sourceRoot": "libs/test/src",
"targets": {},
"targets": {
"test": {},
},
}
`);
expect(tree.exists('libs/test/project.json')).toBeTruthy();
});

it('should add a comment to show project details when targets are missing', () => {
addProjectConfiguration(tree, 'test', {
...projectConfiguration,
targets: {},
});

expect(readProjectConfiguration(tree, 'test')).toMatchInlineSnapshot(`
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"name": "test",
"root": "libs/test",
"sourceRoot": "libs/test/src",
"targets": {},
}
`);

expect(tree.read('libs/test/project.json', 'utf-8')).toMatchInlineSnapshot(`
"{
"name": "test",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/test/src",
"// targets": "to see all targets run: nx show project test --web",
"targets": {}
}
"
`);

// Adding a target removes the "// targets" comment.
updateProjectConfiguration(tree, 'test', {
...projectConfiguration,
targets: {
test: {},
},
});

expect(tree.read('libs/test/project.json', 'utf-8')).toMatchInlineSnapshot(`
"{
"name": "test",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/test/src",
"targets": {
"test": {}
}
}
"
`);

// Emptying out targets add "// targets" comment back.
updateProjectConfiguration(tree, 'test', {
...projectConfiguration,
targets: {},
});

expect(tree.read('libs/test/project.json', 'utf-8')).toMatchInlineSnapshot(`
"{
"name": "test",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/test/src",
"// targets": "to see all targets run: nx show project test --web",
"targets": {}
}
"
`);
});

it('should update project.json file when updating a project', () => {
addProjectConfiguration(tree, 'test', projectConfiguration);
const expectedProjectConfig = {
Expand Down
25 changes: 24 additions & 1 deletion packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export function addProjectConfiguration(
}

delete (projectConfiguration as any).$schema;

handleEmptyTargets(projectName, projectConfiguration);

writeJson(tree, projectConfigFile, {
name: projectName,
$schema: getRelativeProjectJsonSchemaPath(tree, projectConfiguration),
Expand Down Expand Up @@ -94,6 +97,7 @@ export function updateProjectConfiguration(
`Cannot update Project ${projectName} at ${projectConfiguration.root}. It either doesn't exist yet, or may not use project.json for configuration. Use \`addProjectConfiguration()\` instead if you want to create a new project.`
);
}
handleEmptyTargets(projectName, projectConfiguration);
writeJson(tree, projectConfigFile, {
name: projectConfiguration.name ?? projectName,
$schema: getRelativeProjectJsonSchemaPath(tree, projectConfiguration),
Expand Down Expand Up @@ -192,7 +196,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
'**/project.json',
'project.json',
...getGlobPatternsFromPackageManagerWorkspaces(tree.root, (p) =>
readJson(tree, p)
readJson(tree, p, { expectComments: true })
),
];
const projectGlobPatterns = configurationGlobs([
Expand Down Expand Up @@ -328,3 +332,22 @@ function toNewFormat(w: any): ProjectsConfigurations {
}
return w;
}

function handleEmptyTargets(
projectName: string,
projectConfiguration: ProjectConfiguration
): void {
if (
projectConfiguration.targets &&
!Object.keys(projectConfiguration.targets).length
) {
// Re-order `targets` to appear after the `// target` comment.
delete projectConfiguration.targets;
projectConfiguration[
'// targets'
] = `to see all targets run: nx show project ${projectName} --web`;
projectConfiguration.targets = {};
} else {
delete projectConfiguration['// targets'];
}
}
2 changes: 2 additions & 0 deletions packages/nx/src/migrations/update-15-1-0/set-project-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { readJson, writeJson } from '../../generators/utils/json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';
import { retrieveProjectConfigurationPaths } from '../../project-graph/utils/retrieve-workspace-files';
import { loadNxPlugins } from '../../project-graph/plugins/internal-api';
import { shutdownPluginWorkers } from '../../project-graph/plugins/plugin-pool';

export default async function (tree: Tree) {
const nxJson = readNxJson(tree);
const projectFiles = await retrieveProjectConfigurationPaths(
tree.root,
await loadNxPlugins(nxJson?.plugins)
);
await shutdownPluginWorkers();
const projectJsons = projectFiles.filter((f) => f.endsWith('project.json'));

for (let f of projectJsons) {
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/plugins/js/utils/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function getTranspiler(compilerOptions: CompilerOptions) {

compilerOptions.lib = ['es2021'];
compilerOptions.module = ts.ModuleKind.CommonJS;
compilerOptions.moduleResolution = ts.ModuleResolutionKind.Node10;
compilerOptions.target = ts.ScriptTarget.ES2021;
compilerOptions.inlineSourceMap = true;
compilerOptions.skipLibCheck = true;
Expand Down
5 changes: 0 additions & 5 deletions packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,6 @@ async function updateProjectGraphWithPlugins(
createDependencyPlugins.map(async (plugin) => {
performance.mark(`${plugin.name}:createDependencies - start`);

// Set this globally to allow plugins to know if they are being called from the project graph creation
global.NX_GRAPH_CREATION = true;

try {
// TODO: we shouldn't have to pass null here
const dependencies = await plugin.createDependencies(null, {
Expand All @@ -326,8 +323,6 @@ async function updateProjectGraphWithPlugins(
throw new Error(message);
}

delete global.NX_GRAPH_CREATION;

performance.mark(`${plugin.name}:createDependencies - end`);
performance.measure(
`${plugin.name}:createDependencies`,
Expand Down
10 changes: 5 additions & 5 deletions packages/nx/src/project-graph/plugins/internal-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type RemotePlugin =
// holding resolved nx plugin objects.
// Allows loaded plugins to not be reloaded when
// referenced multiple times.
export const nxPluginCache: Map<unknown, RemotePlugin> = new Map();
export const nxPluginCache: Map<unknown, Promise<RemotePlugin>> = new Map();

export async function loadNxPlugins(
plugins: PluginConfiguration[],
Expand Down Expand Up @@ -78,12 +78,12 @@ export async function loadNxPlugin(
const cacheKey = JSON.stringify(plugin);

if (nxPluginCache.has(cacheKey)) {
return nxPluginCache.get(cacheKey)!;
return await nxPluginCache.get(cacheKey)!;
}

const loadedPlugin = await loadRemoteNxPlugin(plugin, root);
nxPluginCache.set(cacheKey, loadedPlugin);
return loadedPlugin;
const loadingPlugin = loadRemoteNxPlugin(plugin, root);
nxPluginCache.set(cacheKey, loadingPlugin);
return await loadingPlugin;
}

export async function getDefaultPlugins(root: string) {
Expand Down
62 changes: 36 additions & 26 deletions packages/nx/src/project-graph/plugins/plugin-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,61 +121,43 @@ function createWorkerHandler(
? [
createNodesPattern,
(configFiles, ctx) => {
return new Promise(function (res, rej) {
const tx =
pluginName + ':createNodes:' + performance.now();
const tx = pluginName + ':createNodes:' + performance.now();
return registerPendingPromise(tx, pending, () => {
worker.send(
createMessage({
type: 'createNodes',
payload: { configFiles, context: ctx, tx },
})
);
pending.add(tx);
promiseBank.set(tx, {
promise: this,
resolver: res,
rejecter: rej,
});
});
},
]
: undefined,
createDependencies: result.hasCreateDependencies
? (opts, ctx) => {
return new Promise(function (res, rej) {
const tx = pluginName + ':createNodes:' + performance.now();
const tx =
pluginName + ':createDependencies:' + performance.now();
return registerPendingPromise(tx, pending, () => {
worker.send(
createMessage({
type: 'createDependencies',
payload: { context: ctx, tx },
})
);
pending.add(tx);
promiseBank.set(tx, {
promise: this,
resolver: res,
rejecter: rej,
});
});
}
: undefined,
processProjectGraph: result.hasProcessProjectGraph
? (graph, ctx) => {
return new Promise(function (res, rej) {
const tx =
pluginName + ':processProjectGraph:' + performance.now();
const tx =
pluginName + ':processProjectGraph:' + performance.now();
return registerPendingPromise(tx, pending, () => {
worker.send(
createMessage({
type: 'processProjectGraph',
payload: { graph, ctx, tx },
})
);
pending.add(tx);
promiseBank.set(tx, {
promise: this,
resolver: res,
rejecter: rej,
});
});
}
: undefined,
Expand Down Expand Up @@ -255,3 +237,31 @@ function getPendingPromises(
}
return pendingTxs;
}

function registerPendingPromise(
tx: string,
pending: Set<string>,
callback: () => void
): Promise<any> {
let resolver, rejecter;

const promise = new Promise((res, rej) => {
resolver = res;
rejecter = rej;

callback();
}).then((val) => {
// Remove the promise from the pending set
pending.delete(tx);
// Return the original value
return val;
});

pending.add(tx);
promiseBank.set(tx, {
promise,
resolver,
rejecter,
});
return promise;
}
Loading

0 comments on commit 8f5f146

Please sign in to comment.