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 */
|
||||
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/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,
|
||||
} from './task-hasher';
|
||||
import { fileHasher } from './impl';
|
||||
import { withEnvironmentVariables } from '../../internal-testing-utils/with-environment';
|
||||
|
||||
describe('TaskHasher', () => {
|
||||
const packageJson = {
|
||||
@ -76,8 +77,8 @@ describe('TaskHasher', () => {
|
||||
vol.reset();
|
||||
});
|
||||
|
||||
it('should create task hash', async () => {
|
||||
process.env.TESTENV = 'env123';
|
||||
it('should create task hash', () =>
|
||||
withEnvironmentVariables({ TESTENV: 'env123' }, async () => {
|
||||
const hasher = new InProcessTaskHasher(
|
||||
{
|
||||
parent: [{ file: '/file', hash: 'file.hash' }],
|
||||
@ -100,7 +101,10 @@ describe('TaskHasher', () => {
|
||||
{ runtime: 'echo runtime123' },
|
||||
{ env: 'TESTENV' },
|
||||
{ env: 'NONEXISTENTENV' },
|
||||
{ input: 'default', projects: ['unrelated'] },
|
||||
{
|
||||
input: 'default',
|
||||
projects: ['unrelated', 'tag:some-tag'],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
@ -114,6 +118,15 @@ describe('TaskHasher', () => {
|
||||
targets: { build: {} },
|
||||
},
|
||||
},
|
||||
tagged: {
|
||||
name: 'tagged',
|
||||
type: 'lib',
|
||||
data: {
|
||||
root: 'libs/tagged',
|
||||
targets: { build: {} },
|
||||
tags: ['some-tag'],
|
||||
},
|
||||
},
|
||||
},
|
||||
dependencies: {
|
||||
parent: [],
|
||||
@ -142,13 +155,17 @@ describe('TaskHasher', () => {
|
||||
expect(hash.value).toContain('env123');
|
||||
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({
|
||||
'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',
|
||||
'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"]}}}',
|
||||
'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}/.gitignore': '',
|
||||
'{workspaceRoot}/.nxignore': '',
|
||||
@ -157,7 +174,7 @@ describe('TaskHasher', () => {
|
||||
'env:TESTENV': 'env123',
|
||||
'env:NONEXISTENTENV': '',
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should hash task where the project has dependencies', async () => {
|
||||
const hasher = new InProcessTaskHasher(
|
||||
@ -359,7 +376,9 @@ describe('TaskHasher', () => {
|
||||
});
|
||||
|
||||
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(
|
||||
{
|
||||
parent: [
|
||||
@ -462,8 +481,12 @@ describe('TaskHasher', () => {
|
||||
expect(childHash.details.nodes['{workspaceRoot}/global1']).toEqual(
|
||||
'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();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
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 { hashArray } from './impl';
|
||||
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 =
|
||||
| { fileset: string }
|
||||
@ -495,7 +495,11 @@ class TaskHasherImpl {
|
||||
): Promise<PartialHash[]> {
|
||||
const partialHashes: Promise<PartialHash[]>[] = [];
|
||||
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(
|
||||
this.nxJson,
|
||||
this.projectGraph.nodes[project]
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { splitArgsIntoNxArgsAndOverrides } from './command-line-utils';
|
||||
import { withEnvironmentVariables as withEnvironment } from '../../internal-testing-utils/with-environment';
|
||||
|
||||
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