feat(core): add matching support to inputs projects (#17255)
This commit is contained in:
parent
d0421e7aae
commit
471616b04d
@ -1,3 +1,4 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-restricted-imports */
|
/* eslint-disable @typescript-eslint/no-restricted-imports */
|
||||||
export * from 'nx/internal-testing-utils/assert-valid-migrations';
|
export * from 'nx/internal-testing-utils/assert-valid-migrations';
|
||||||
export * from 'nx/internal-testing-utils/run-migration-against-this-workspace';
|
export * from 'nx/internal-testing-utils/run-migration-against-this-workspace';
|
||||||
|
export * from 'nx/internal-testing-utils/with-environment';
|
||||||
|
|||||||
29
packages/nx/internal-testing-utils/with-environment.ts
Normal file
29
packages/nx/internal-testing-utils/with-environment.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
export function withEnvironmentVariables(
|
||||||
|
env: Record<string, string>,
|
||||||
|
callback: () => void
|
||||||
|
): void;
|
||||||
|
export function withEnvironmentVariables(
|
||||||
|
env: Record<string, string>,
|
||||||
|
callback: () => Promise<void>
|
||||||
|
): Promise<void>;
|
||||||
|
export function withEnvironmentVariables(
|
||||||
|
env: Record<string, string>,
|
||||||
|
callback: () => void | Promise<void>
|
||||||
|
): void | Promise<void> {
|
||||||
|
const originalValues: Record<string, string> = {};
|
||||||
|
for (const key in env) {
|
||||||
|
originalValues[key] = process.env[key];
|
||||||
|
process.env[key] = env[key];
|
||||||
|
}
|
||||||
|
const cleanup = () => {
|
||||||
|
for (const key in env) {
|
||||||
|
process.env[key] = originalValues[key];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const p = callback();
|
||||||
|
if (p instanceof Promise) {
|
||||||
|
return p.finally(cleanup);
|
||||||
|
} else {
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -28,6 +28,7 @@ import {
|
|||||||
InProcessTaskHasher,
|
InProcessTaskHasher,
|
||||||
} from './task-hasher';
|
} from './task-hasher';
|
||||||
import { fileHasher } from './impl';
|
import { fileHasher } from './impl';
|
||||||
|
import { withEnvironmentVariables } from '../../internal-testing-utils/with-environment';
|
||||||
|
|
||||||
describe('TaskHasher', () => {
|
describe('TaskHasher', () => {
|
||||||
const packageJson = {
|
const packageJson = {
|
||||||
@ -76,8 +77,8 @@ describe('TaskHasher', () => {
|
|||||||
vol.reset();
|
vol.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create task hash', async () => {
|
it('should create task hash', () =>
|
||||||
process.env.TESTENV = 'env123';
|
withEnvironmentVariables({ TESTENV: 'env123' }, async () => {
|
||||||
const hasher = new InProcessTaskHasher(
|
const hasher = new InProcessTaskHasher(
|
||||||
{
|
{
|
||||||
parent: [{ file: '/file', hash: 'file.hash' }],
|
parent: [{ file: '/file', hash: 'file.hash' }],
|
||||||
@ -100,7 +101,10 @@ describe('TaskHasher', () => {
|
|||||||
{ runtime: 'echo runtime123' },
|
{ runtime: 'echo runtime123' },
|
||||||
{ env: 'TESTENV' },
|
{ env: 'TESTENV' },
|
||||||
{ env: 'NONEXISTENTENV' },
|
{ env: 'NONEXISTENTENV' },
|
||||||
{ input: 'default', projects: ['unrelated'] },
|
{
|
||||||
|
input: 'default',
|
||||||
|
projects: ['unrelated', 'tag:some-tag'],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -114,6 +118,15 @@ describe('TaskHasher', () => {
|
|||||||
targets: { build: {} },
|
targets: { build: {} },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
tagged: {
|
||||||
|
name: 'tagged',
|
||||||
|
type: 'lib',
|
||||||
|
data: {
|
||||||
|
root: 'libs/tagged',
|
||||||
|
targets: { build: {} },
|
||||||
|
tags: ['some-tag'],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
dependencies: {
|
dependencies: {
|
||||||
parent: [],
|
parent: [],
|
||||||
@ -142,13 +155,17 @@ describe('TaskHasher', () => {
|
|||||||
expect(hash.value).toContain('env123');
|
expect(hash.value).toContain('env123');
|
||||||
expect(hash.value).toContain('filec.hash');
|
expect(hash.value).toContain('filec.hash');
|
||||||
|
|
||||||
expect(hash.details.command).toEqual('parent|build||{"prop":"prop-value"}');
|
expect(hash.details.command).toEqual(
|
||||||
|
'parent|build||{"prop":"prop-value"}'
|
||||||
|
);
|
||||||
expect(hash.details.nodes).toEqual({
|
expect(hash.details.nodes).toEqual({
|
||||||
'parent:{projectRoot}/**/*':
|
'parent:{projectRoot}/**/*':
|
||||||
'/file|file.hash|{"root":"libs/parent","targets":{"build":{"executor":"nx:run-commands","inputs":["default","^default",{"runtime":"echo runtime123"},{"env":"TESTENV"},{"env":"NONEXISTENTENV"},{"input":"default","projects":["unrelated"]}]}}}|{"compilerOptions":{"paths":{"@nx/parent":["libs/parent/src/index.ts"],"@nx/child":["libs/child/src/index.ts"]}}}',
|
'/file|file.hash|{"root":"libs/parent","targets":{"build":{"executor":"nx:run-commands","inputs":["default","^default",{"runtime":"echo runtime123"},{"env":"TESTENV"},{"env":"NONEXISTENTENV"},{"input":"default","projects":["unrelated","tag:some-tag"]}]}}}|{"compilerOptions":{"paths":{"@nx/parent":["libs/parent/src/index.ts"],"@nx/child":["libs/child/src/index.ts"]}}}',
|
||||||
target: 'nx:run-commands',
|
target: 'nx:run-commands',
|
||||||
'unrelated:{projectRoot}/**/*':
|
'unrelated:{projectRoot}/**/*':
|
||||||
'libs/unrelated/filec.ts|filec.hash|{"root":"libs/unrelated","targets":{"build":{}}}|{"compilerOptions":{"paths":{"@nx/parent":["libs/parent/src/index.ts"],"@nx/child":["libs/child/src/index.ts"]}}}',
|
'libs/unrelated/filec.ts|filec.hash|{"root":"libs/unrelated","targets":{"build":{}}}|{"compilerOptions":{"paths":{"@nx/parent":["libs/parent/src/index.ts"],"@nx/child":["libs/child/src/index.ts"]}}}',
|
||||||
|
'tagged:{projectRoot}/**/*':
|
||||||
|
'{"root":"libs/tagged","targets":{"build":{}},"tags":["some-tag"]}|{"compilerOptions":{"paths":{"@nx/parent":["libs/parent/src/index.ts"],"@nx/child":["libs/child/src/index.ts"]}}}',
|
||||||
'{workspaceRoot}/nx.json': 'nx.json.hash',
|
'{workspaceRoot}/nx.json': 'nx.json.hash',
|
||||||
'{workspaceRoot}/.gitignore': '',
|
'{workspaceRoot}/.gitignore': '',
|
||||||
'{workspaceRoot}/.nxignore': '',
|
'{workspaceRoot}/.nxignore': '',
|
||||||
@ -157,7 +174,7 @@ describe('TaskHasher', () => {
|
|||||||
'env:TESTENV': 'env123',
|
'env:TESTENV': 'env123',
|
||||||
'env:NONEXISTENTENV': '',
|
'env:NONEXISTENTENV': '',
|
||||||
});
|
});
|
||||||
});
|
}));
|
||||||
|
|
||||||
it('should hash task where the project has dependencies', async () => {
|
it('should hash task where the project has dependencies', async () => {
|
||||||
const hasher = new InProcessTaskHasher(
|
const hasher = new InProcessTaskHasher(
|
||||||
@ -359,7 +376,9 @@ describe('TaskHasher', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to handle multiple filesets per project', async () => {
|
it('should be able to handle multiple filesets per project', async () => {
|
||||||
process.env.MY_TEST_HASH_ENV = 'MY_TEST_HASH_ENV_VALUE';
|
withEnvironmentVariables(
|
||||||
|
{ MY_TEST_HASH_ENV: 'MY_TEST_HASH_ENV_VALUE' },
|
||||||
|
async () => {
|
||||||
const hasher = new InProcessTaskHasher(
|
const hasher = new InProcessTaskHasher(
|
||||||
{
|
{
|
||||||
parent: [
|
parent: [
|
||||||
@ -462,8 +481,12 @@ describe('TaskHasher', () => {
|
|||||||
expect(childHash.details.nodes['{workspaceRoot}/global1']).toEqual(
|
expect(childHash.details.nodes['{workspaceRoot}/global1']).toEqual(
|
||||||
'global1.hash'
|
'global1.hash'
|
||||||
);
|
);
|
||||||
expect(childHash.details.nodes['{workspaceRoot}/global2']).toBe(undefined);
|
expect(childHash.details.nodes['{workspaceRoot}/global2']).toBe(
|
||||||
|
undefined
|
||||||
|
);
|
||||||
expect(childHash.details.nodes['env:MY_TEST_HASH_ENV']).toBeUndefined();
|
expect(childHash.details.nodes['env:MY_TEST_HASH_ENV']).toBeUndefined();
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use targetDefaults from nx.json', async () => {
|
it('should use targetDefaults from nx.json', async () => {
|
||||||
|
|||||||
@ -15,7 +15,7 @@ import { DaemonClient } from '../daemon/client/client';
|
|||||||
import { FileHasher } from './impl/file-hasher-base';
|
import { FileHasher } from './impl/file-hasher-base';
|
||||||
import { hashArray } from './impl';
|
import { hashArray } from './impl';
|
||||||
import { createProjectRootMappings } from '../project-graph/utils/find-project-for-path';
|
import { createProjectRootMappings } from '../project-graph/utils/find-project-for-path';
|
||||||
import { registerPluginTSTranspiler } from '../utils/nx-plugin';
|
import { findMatchingProjects } from '../utils/find-matching-projects';
|
||||||
|
|
||||||
type ExpandedSelfInput =
|
type ExpandedSelfInput =
|
||||||
| { fileset: string }
|
| { fileset: string }
|
||||||
@ -495,7 +495,11 @@ class TaskHasherImpl {
|
|||||||
): Promise<PartialHash[]> {
|
): Promise<PartialHash[]> {
|
||||||
const partialHashes: Promise<PartialHash[]>[] = [];
|
const partialHashes: Promise<PartialHash[]>[] = [];
|
||||||
for (const input of projectInputs) {
|
for (const input of projectInputs) {
|
||||||
for (const project of input.projects) {
|
const projects = findMatchingProjects(
|
||||||
|
input.projects,
|
||||||
|
this.projectGraph.nodes
|
||||||
|
);
|
||||||
|
for (const project of projects) {
|
||||||
const namedInputs = getNamedInputs(
|
const namedInputs = getNamedInputs(
|
||||||
this.nxJson,
|
this.nxJson,
|
||||||
this.projectGraph.nodes[project]
|
this.projectGraph.nodes[project]
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { splitArgsIntoNxArgsAndOverrides } from './command-line-utils';
|
import { splitArgsIntoNxArgsAndOverrides } from './command-line-utils';
|
||||||
|
import { withEnvironmentVariables as withEnvironment } from '../../internal-testing-utils/with-environment';
|
||||||
|
|
||||||
jest.mock('../project-graph/file-utils');
|
jest.mock('../project-graph/file-utils');
|
||||||
|
|
||||||
@ -458,15 +459,3 @@ describe('splitArgs', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function withEnvironment(env: Record<string, string>, callback: () => void) {
|
|
||||||
const originalValues: Record<string, string> = {};
|
|
||||||
for (const key in env) {
|
|
||||||
originalValues[key] = process.env[key];
|
|
||||||
process.env[key] = env[key];
|
|
||||||
}
|
|
||||||
callback();
|
|
||||||
for (const key in env) {
|
|
||||||
process.env[key] = originalValues[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user