Skip to content

Commit

Permalink
fix(core): remove leading ./ when normalizing root project outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Dec 10, 2022
1 parent 7daa2eb commit 40b39b2
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/shared/angular-standalone-tutorial/3-task-running.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Here's the `project.json` file for your `shared-ui` project:
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "shared/ui/jest.config.ts",
"passWithNoTests": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Here are the outputs defined for the `shared-ui` project:
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "shared/ui/jest.config.ts",
"passWithNoTests": true
Expand Down
2 changes: 1 addition & 1 deletion docs/shared/angular-tutorial/3-task-running.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Here's the `project.json` file for your `common-ui` project:
"targets": {
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "libs/common-ui/jest.config.ts",
"passWithNoTests": true
Expand Down
2 changes: 1 addition & 1 deletion docs/shared/angular-tutorial/4-workspace-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Outputs are defined for every target in your workspace:
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "libs/products/jest.config.ts",
"passWithNoTests": true
Expand Down
2 changes: 1 addition & 1 deletion docs/shared/mental-model/large-tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -14215,7 +14215,7 @@
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "graph/ui-graph/jest.config.ts",
"passWithNoTests": true
Expand Down
2 changes: 0 additions & 2 deletions e2e/jest/src/jest-root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ describe('Jest root projects', () => {

it('should test root level app projects', async () => {
runCLI(`generate @nrwl/angular:app ${myapp} --rootProject=true`);

const rootProjectTestResults = await runCLIAsync(`test ${myapp}`);

expect(rootProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
Expand Down
2 changes: 1 addition & 1 deletion e2e/nx-run/src/affected-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ describe('Nx Affected and Graph Tests', () => {
target: 'test',
},
command: `${runNx} run ${myapp}:test`,
outputs: [`coverage/apps/${myapp}`],
outputs: [`coverage/${myapp}`],
});
compareTwoArrays(resWithTarget.projects, [myapp]);

Expand Down
2 changes: 1 addition & 1 deletion graph/ui-graph/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "graph/ui-graph/jest.config.ts",
"passWithNoTests": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Object {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
},
},
Expand Down Expand Up @@ -385,7 +385,7 @@ Object {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Object {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
},
},
Expand Down Expand Up @@ -393,7 +393,7 @@ Object {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Object {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('jestProject', () => {
const lib1 = readProjectConfiguration(tree, 'lib1');
expect(lib1.targets.test).toEqual({
executor: '@nrwl/jest:jest',
outputs: ['{workspaceRoot}/coverage/{projectRoot}'],
outputs: ['{workspaceRoot}/coverage/{projectName}'],
options: {
jestConfig: 'libs/lib1/jest.config.ts',
passWithNoTests: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function updateWorkspace(tree: Tree, options: JestProjectSchema) {
projectConfig.targets.test = {
executor: '@nrwl/jest:jest',
outputs: [
joinPathFragments('{workspaceRoot}', 'coverage', '{projectRoot}'),
joinPathFragments('{workspaceRoot}', 'coverage', '{projectName}'),
],
options: {
jestConfig: joinPathFragments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('@nrwl/linter:workspace-rules-project', () => {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/nest/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('lib', () => {
});
expect(workspaceJson.projects[libFileName].architect.test).toEqual({
builder: '@nrwl/jest:jest',
outputs: [`{workspaceRoot}/coverage/{projectRoot}`],
outputs: [`{workspaceRoot}/coverage/{projectName}`],
options: {
jestConfig: `libs/${libFileName}/jest.config.ts`,
passWithNoTests: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('lib', () => {
});
expect(workspaceJson.projects['my-lib'].architect.test).toEqual({
builder: '@nrwl/jest:jest',
outputs: ['{workspaceRoot}/coverage/{projectRoot}'],
outputs: ['{workspaceRoot}/coverage/{projectName}'],
options: {
jestConfig: 'libs/my-lib/jest.config.ts',
passWithNoTests: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-plugin/src/generators/plugin/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('NxPlugin Plugin Generator', () => {
});
expect(project.targets.test).toEqual({
executor: '@nrwl/jest:jest',
outputs: ['{workspaceRoot}/coverage/{projectRoot}'],
outputs: ['{workspaceRoot}/coverage/{projectName}'],
options: {
jestConfig: 'libs/my-plugin/jest.config.ts',
passWithNoTests: true,
Expand Down
51 changes: 50 additions & 1 deletion packages/nx/src/tasks-runner/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('utils', () => {
).toEqual(['one', 'myapp/two', 'myapp/three']);
});

it('should interpolate {projectRoot} when it is not in front', () => {
it('should interpolate {projectRoot} when it is not at the beginning', () => {
expect(
getOutputsForTargetAndConfiguration(
task,
Expand All @@ -67,6 +67,55 @@ describe('utils', () => {
).toEqual(['dist/myapp']);
});

it('should throw when {workspaceRoot} is used not at the beginning', () => {
expect(() =>
getOutputsForTargetAndConfiguration(
task,
getNode({
outputs: ['test/{workspaceRoot}/dist'],
})
)
).toThrow();
});

it('should interpolate {projectRoot} = . by removing the slash after it', () => {
const data = {
name: 'myapp',
type: 'app',
data: {
root: '.',
targets: {
build: {
outputs: ['{projectRoot}/dist'],
},
},
files: [],
},
};
expect(getOutputsForTargetAndConfiguration(task, data as any)).toEqual([
'dist',
]);
});

it('should throw when {projectRoot} is used not at the beginning and the value is .', () => {
const data = {
name: 'myapp',
type: 'app',
data: {
root: '.',
targets: {
build: {
outputs: ['test/{projectRoot}'],
},
},
files: [],
},
};
expect(() =>
getOutputsForTargetAndConfiguration(task, data as any)
).toThrow();
});

it('should support interpolation based on options', () => {
expect(
getOutputsForTargetAndConfiguration(
Expand Down
41 changes: 28 additions & 13 deletions packages/nx/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { flatten } from 'flat';
import { output } from '../utils/output';
import { Workspaces } from '../config/workspaces';
import { mergeNpmScriptsWithTargets } from '../utils/project-graph-utils';
Expand Down Expand Up @@ -183,19 +182,35 @@ export function getOutputsForTargetAndConfiguration(
}

export function interpolate(template: string, data: any): string {
return template
.replace('{workspaceRoot}/', '')
.replace(/{([\s\S]+?)}/g, (match: string) => {
let value = data;
let path = match.slice(1, -1).trim().split('.');
for (let idx = 0; idx < path.length; idx++) {
if (!value[path[idx]]) {
return match;
}
value = value[path[idx]];
if (template.includes('{workspaceRoot}', 1)) {
throw new Error(
`Output '${template}' is invalid. {workspaceRoot} can only be used at the beginning of the expression.`
);
}

if (data.projectRoot == '.' && template.includes('{projectRoot}', 1)) {
throw new Error(
`Output '${template}' is invalid. When {projectRoot} is '.', it can only be used at the beginning of the expression.`
);
}

let res = template.replace('{workspaceRoot}/', '');

if (data.projectRoot == '.') {
res = template.replace('{projectRoot}/', '');
}

return res.replace(/{([\s\S]+?)}/g, (match: string) => {
let value = data;
let path = match.slice(1, -1).trim().split('.');
for (let idx = 0; idx < path.length; idx++) {
if (!value[path[idx]]) {
return match;
}
return value;
});
value = value[path[idx]];
}
return value;
});
}

export function getExecutorNameForTask(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ exports[`@nrwl/vite:configuration transform React app to use Vite should transfo
\\"test\\": {
\\"builder\\": \\"@nrwl/jest:jest\\",
\\"outputs\\": [
\\"{workspaceRoot}/coverage/{projectRoot}\\"
\\"{workspaceRoot}/coverage/{projectName}\\"
],
\\"options\\": {
\\"jestConfig\\": \\"apps/my-test-react-app/jest.config.ts\\",
Expand Down Expand Up @@ -148,7 +148,7 @@ exports[`@nrwl/vite:configuration transform Web app to use Vite should transform
\\"test\\": {
\\"builder\\": \\"@nrwl/jest:jest\\",
\\"outputs\\": [
\\"{workspaceRoot}/coverage/{projectRoot}\\"
\\"{workspaceRoot}/coverage/{projectName}\\"
],
\\"options\\": {
\\"jestConfig\\": \\"apps/my-test-web-app/jest.config.ts\\",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/generators/vitest/vitest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('vitest generator', () => {
"passWithNoTests": true,
},
"outputs": Array [
"{workspaceRoot}/coverage/{projectRoot}",
"{workspaceRoot}/coverage/{projectName}",
],
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "apps/my-test-react-app/jest.config.ts",
"passWithNoTests": true
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/utils/test-files/web-project.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": ["{workspaceRoot}/coverage/{projectName}"],
"options": {
"jestConfig": "apps/my-test-web-app/jest.config.ts",
"passWithNoTests": true
Expand Down

0 comments on commit 40b39b2

Please sign in to comment.