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

fix(core): migrate old invalid glob syntax in target outputs #18191

Merged
merged 7 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions docs/shared/reference/project-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ Sometimes, multiple targets might write to the same directory. When possible it
}
```

But if the above is not possible, globs (parsed with the [minimatch](https://github.com/isaacs/minimatch) library) can be specified as outputs to only cache a set of files rather than the whole directory.
But if the above is not possible, globs (parsed by the [GlobSet](https://docs.rs/globset/0.4.5/globset/#syntax) Rust library) can be specified as outputs to only cache a set of files rather than the whole directory.

```json
{
"targets": {
"build-js": {
"outputs": ["{workspaceRoot}/dist/libs/mylib/**/*.js"]
"outputs": ["{workspaceRoot}/dist/libs/mylib/**/*.{js,map}"]
},
"build-css": {
"outputs": ["{workspaceRoot}/dist/libs/mylib/**/*.css"]
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
"version": "16.2.0-beta.0",
"description": "Remove outputPath from run commands",
"implementation": "./src/migrations/update-16-2-0/remove-run-commands-output-path"
},
"16.5.4-update-output-globs": {
"cli": "nx",
"version": "16.5.4-beta.0",
"description": "Update outdated non-standard globs to unix standard",
"implementation": "./src/migrations/update-16-5-4/update-output-globs"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import { TargetConfiguration } from '../../config/workspace-json-project-json';
import {
addProjectConfiguration,
readProjectConfiguration,
} from '../../generators/utils/project-configuration';
import updateOutputsGlobs from './update-output-globs';
import { readJson, updateJson } from '../../generators/utils/json';
import { NxJsonConfiguration } from '../../config/nx-json';

describe('update output globs', () => {
it('should update output globs', () => {
const tree = createTreeWithEmptyWorkspace();
const targets: Record<string, TargetConfiguration> = {
build: {
outputs: ['{options.outputPath}', 'dist/apps/my-app/*.(js|map|ts)'],
},
lint: {},
test: {
outputs: ['dist/apps/my-app/main.(js|map|ts)'],
},
run: {
outputs: ['dist/apps/my-app'],
},
};
addProjectConfiguration(tree, 'my-app', {
root: 'apps/my-app',
targets,
});

updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => {
json.targetDefaults = {
lint: {
outputs: ['dist/apps/my-app', '*.(js|map|ts)'],
},
};
return json;
});

updateOutputsGlobs(tree);

const migratedTargets = readProjectConfiguration(tree, 'my-app').targets;
expect(migratedTargets).toMatchInlineSnapshot(`
{
"build": {
"outputs": [
"{options.outputPath}",
"dist/apps/my-app/*.{js,map,ts}",
],
},
"lint": {},
"run": {
"outputs": [
"dist/apps/my-app",
],
},
"test": {
"outputs": [
"dist/apps/my-app/main.{js,map,ts}",
],
},
}
`);

const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.targetDefaults).toMatchInlineSnapshot(`
{
"lint": {
"outputs": [
"dist/apps/my-app",
"*.{js,map,ts}",
],
},
}
`);
});
});
58 changes: 58 additions & 0 deletions packages/nx/src/migrations/update-16-5-4/update-output-globs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Tree } from '../../generators/tree';
import {
getProjects,
updateProjectConfiguration,
} from '../../generators/utils/project-configuration';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';
import { TargetConfiguration } from '../../config/workspace-json-project-json';
import { updateJson } from '../../generators/utils/json';
import { NxJsonConfiguration } from '../../config/nx-json';

function replaceOutputs(targetConfiguration: TargetConfiguration<any>) {
let outputs = [];
for (const output of targetConfiguration.outputs ?? []) {
// replace {projectRoot}/folder/*.(js|map|ts) to {projectRoot}/folder/*.{js,map,ts}
const regex = /\(([^)]+)\)/g;
const newOutput = output.replace(regex, (match, group1) => {
let replacements = group1.split('|').join(',');
return `{${replacements}}`;
});

outputs.push(newOutput);
}
return outputs;
}

export default async function updateOutputsGlobs(tree: Tree) {
for (const [projectName, projectConfiguration] of getProjects(
tree
).entries()) {
for (const [targetName, targetConfiguration] of Object.entries(
projectConfiguration.targets
Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
)) {
if (!Array.isArray(targetConfiguration.outputs)) {
continue;
}

targetConfiguration.outputs = replaceOutputs(targetConfiguration);
Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
}
updateProjectConfiguration(tree, projectName, projectConfiguration);
}

if (tree.exists('nx.json')) {
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => {
for (const [, targetConfiguration] of Object.entries(
json.targetDefaults ?? {}
)) {
if (!Array.isArray(targetConfiguration.outputs)) {
continue;
}

targetConfiguration.outputs = replaceOutputs(targetConfiguration);
Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
}
return json;
});
}

await formatChangedFilesWithPrettierIfAvailable(tree);
}
16 changes: 16 additions & 0 deletions packages/nx/src/native/cache/expand_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ mod test {
.child("nx.darwin-arm64.node")
.touch()
.unwrap();
temp.child("multi").child("file.js").touch().unwrap();
temp.child("multi").child("src.ts").touch().unwrap();
temp.child("multi").child("file.map").touch().unwrap();
temp.child("multi").child("file.txt").touch().unwrap();
temp
}
#[test]
Expand All @@ -80,4 +84,16 @@ mod test {
]
);
}

#[test]
fn should_handle_multiple_extensions() {
let temp = setup_fs();
let entries = vec!["multi/*.{js,map,ts}".to_string()];
let mut result = expand_outputs(temp.display().to_string(), entries).unwrap();
result.sort();
assert_eq!(
result,
vec!["multi/file.js", "multi/file.map", "multi/src.ts"]
);
}
}
6 changes: 5 additions & 1 deletion packages/nx/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ export function getOutputsForTargetAndConfiguration(
options,
});
})
.filter((output) => !!output && !output.match(/{.*}/));
.filter(
(output) =>
!!output &&
!output.match(/{(projectRoot|workspaceRoot|(options\.*))}/)
);
}

// Keep backwards compatibility in case `outputs` doesn't exist
Expand Down