Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): pattern matching for target defaults #26870

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ describe('project-configuration-utils', () => {
key: 'default-value-for-targetname',
},
},
'e2e-ci--*': {
options: {
key: 'default-value-for-e2e-ci',
},
},
'e2e-ci--file-*': {
options: {
key: 'default-value-for-e2e-ci-file',
},
},
};

it('should prefer executor key', () => {
Expand Down Expand Up @@ -61,6 +71,14 @@ describe('project-configuration-utils', () => {
).toBeNull();
});

it('should return longest matching target', () => {
expect(
// This matches both 'e2e-ci--*' and 'e2e-ci--file-*', we expect the first match to be returned.
readTargetDefaultsForTarget('e2e-ci--file-foo', targetDefaults, null)
.options['key']
).toEqual('default-value-for-e2e-ci-file');
});

it('should not merge top level properties for incompatible targets', () => {
expect(
mergeTargetConfigurations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
AggregateCreateNodesError,
} from '../error-types';
import { CreateNodesResult } from '../plugins';
import { isGlobPattern } from '../../utils/globs';

export type SourceInformation = [file: string | null, plugin: string];
export type ConfigurationSourceMaps = Record<
Expand Down Expand Up @@ -1036,10 +1037,27 @@ export function readTargetDefaultsForTarget(
// If not, use build if it is present.
const key = [executor, targetName].find((x) => targetDefaults?.[x]);
return key ? targetDefaults?.[key] : null;
} else {
} else if (targetDefaults?.[targetName]) {
// If the executor is not defined, the only key we have is the target name.
return targetDefaults?.[targetName];
}

let matchingTargetDefaultKey: string | null = null;
for (const key in targetDefaults ?? {}) {
if (isGlobPattern(key) && minimatch(targetName, key)) {
if (
!matchingTargetDefaultKey ||
matchingTargetDefaultKey.length < key.length
) {
matchingTargetDefaultKey = key;
}
}
}
if (matchingTargetDefaultKey) {
return targetDefaults[matchingTargetDefaultKey];
}

return {};
}

function createRootMap(projectRootMap: Record<string, ProjectConfiguration>) {
Expand Down
8 changes: 3 additions & 5 deletions packages/nx/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import { splitByColons } from '../utils/split-target';
import { getExecutorInformation } from '../command-line/run/executor-utils';
import { CustomHasher, ExecutorConfig } from '../config/misc-interfaces';
import { readProjectsConfigurationFromProjectGraph } from '../project-graph/project-graph';
import {
GLOB_CHARACTERS,
findMatchingProjects,
} from '../utils/find-matching-projects';
import { findMatchingProjects } from '../utils/find-matching-projects';
import { minimatch } from 'minimatch';
import { isGlobPattern } from '../utils/globs';

export type NormalizedTargetDependencyConfig = TargetDependencyConfig & {
projects: string[];
Expand Down Expand Up @@ -122,7 +120,7 @@ export function expandWildcardTargetConfiguration(
dependencyConfig: NormalizedTargetDependencyConfig,
allTargetNames: string[]
): NormalizedTargetDependencyConfig[] {
if (!GLOB_CHARACTERS.some((char) => dependencyConfig.target.includes(char))) {
if (!isGlobPattern(dependencyConfig.target)) {
return [dependencyConfig];
}
let cache = patternResultCache.get(allTargetNames);
Expand Down
10 changes: 3 additions & 7 deletions packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { minimatch } from 'minimatch';
import type { ProjectGraphProjectNode } from '../config/project-graph';
import { isGlobPattern } from './globs';

const validPatternTypes = [
'name', // Pattern is based on the project's name
Expand All @@ -18,11 +19,6 @@ interface ProjectPattern {
value: string;
}

/**
* The presence of these characters in a string indicates that it might be a glob pattern.
*/
export const GLOB_CHARACTERS = ['*', '|', '{', '}', '(', ')'];

/**
* Find matching project names given a list of potential project names or globs.
*
Expand Down Expand Up @@ -169,7 +165,7 @@ function addMatchingProjectsByName(
return;
}

if (!GLOB_CHARACTERS.some((c) => pattern.value.includes(c))) {
if (!isGlobPattern(pattern.value)) {
return;
}

Expand Down Expand Up @@ -204,7 +200,7 @@ function addMatchingProjectsByTag(
continue;
}

if (!GLOB_CHARACTERS.some((c) => pattern.value.includes(c))) {
if (!isGlobPattern(pattern.value)) {
continue;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/nx/src/utils/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ export function combineGlobPatterns(...patterns: (string | string[])[]) {
const p = patterns.flat();
return p.length > 1 ? '{' + p.join(',') + '}' : p.length === 1 ? p[0] : '';
}

export const GLOB_CHARACTERS = new Set(['*', '|', '{', '}', '(', ')', '[']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all the characters I found when doing the whole glob conversion stuff in the Rust side. Are they needed here as well (like the ?, @, etc)?

pub(crate) fn contains_glob_pattern(value: &str) -> bool {
value.contains('!')
|| value.contains('?')
|| value.contains('@')
|| value.contains('+')
|| value.contains('*')
|| value.contains('|')
|| value.contains(',')
|| value.contains('{')
|| value.contains('}')
|| value.contains('[')
|| value.contains(']')
|| value.contains('(')
|| value.contains(')')
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so... ?, @, +, ! are only valid in combination with paraenthises


export function isGlobPattern(pattern: string) {
for (const c of pattern) {
if (GLOB_CHARACTERS.has(c)) {
return true;
}
}
return false;
}