Skip to content

Commit

Permalink
fix(core): nx v15 prefers output paths that start with {workspaceRoot}
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Oct 24, 2022
1 parent 8ff832a commit 90b3aab
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 243 deletions.
1 change: 1 addition & 0 deletions demo/apps/web-frontend/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default {
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage//demo/apps/web-frontend',
testEnvironment: 'jsdom',
};
17 changes: 3 additions & 14 deletions e2e/core-e2e/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,35 +376,24 @@ describe('nx-dotnet e2e', () => {
});
});

it('should work with workspace.json + project.json', () => {
const relativeProjectPath = joinPathFragments('apps', api);
it('should work with project.json', () => {
writeFileSync(
join(projectFolder, 'project.json'),
JSON.stringify({
root: relativeProjectPath,
targets: {},
}),
);
updateFile('workspace.json', (c) => {
const json = JSON.parse(c);
json.projects[api] = relativeProjectPath;
return JSON.stringify(json, null, 2);
});
expect(() => runNxCommand(`build ${api}`)).not.toThrow();
});

it('should work without workspace.json or project.json', () => {
const workspaceJsonContents = readFile('workspace.json');
unlinkSync(join(e2eDir, 'workspace.json'));

it('should work without project.json', () => {
const projectJsonContents = readFile(
joinPathFragments('apps', api, 'project.json'),
);
unlinkSync(join(projectFolder, 'project.json'));

expect(() => runNxCommand(`build ${api}`)).not.toThrow();

writeFileSync(join(e2eDir, 'workspace.json'), workspaceJsonContents);

writeFileSync(join(projectFolder, 'project.json'), projectJsonContents);
});
});
Expand Down
40 changes: 1 addition & 39 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import * as path from 'path';
import { dirSync } from 'tmp';

import isCI = require('is-ci');
import * as isCI from 'is-ci';
import { workspaceConfigName } from 'nx/src/config/workspaces';
import { detectPackageManager } from '@nrwl/devkit';
interface RunCmdOpts {
Expand Down Expand Up @@ -143,11 +143,6 @@ export function runCLI(
console.log(r);
}

const needsMaxWorkers = /g.*(express|nest|node|web|react):app.*/;
if (command && needsMaxWorkers.test(command)) {
setMaxWorkers();
}

return r;
} catch (e: any) {
if (opts.silenceError) {
Expand Down Expand Up @@ -181,39 +176,6 @@ export function runCommand(command: string): string {
}
}

/**
* Sets maxWorkers in CI on all projects that require it
* so that it doesn't try to run it with 34 workers
*
* maxWorkers required for: node, web, jest
*/
function setMaxWorkers() {
if (isCI) {
const workspaceFile = workspaceConfigName(tmpProjPath());
const workspace = readJson(workspaceFile);

Object.keys(workspace.projects).forEach((appName) => {
const project = workspace.projects[appName];
const { build } = project.targets ?? project.architect;

if (!build) {
return;
}

const executor = build.builder ?? build.executor;
if (
executor.startsWith('@nrwl/node') ||
executor.startsWith('@nrwl/web') ||
executor.startsWith('@nrwl/jest')
) {
build.options.maxWorkers = 4;
}
});

updateFile(workspaceFile, JSON.stringify(workspace));
}
}

export function createFile(f: string, content: string = ''): void {
const path = tmpProjPath(f);
createFileSync(path);
Expand Down
17 changes: 3 additions & 14 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { getJestProjects } from '@nrwl/jest';

export default {
projects: [
'<rootDir>/e2e/core-e2e',
'<rootDir>/e2e/nx-ghpages-e2e',
'<rootDir>/e2e/nxdoc-e2e',
'<rootDir>/packages/core',
'<rootDir>/packages/dotnet',
'<rootDir>/packages/nx-ghpages',
'<rootDir>/packages/nxdoc',
'<rootDir>/packages/utils',
'<rootDir>/smoke/core',
'<rootDir>/demo/libs/generated/webapi-types',
'<rootDir>/demo/apps/web-frontend',
'<rootDir>/demo/apps/web-frontend',
],
projects: getJestProjects(),
};
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const nxPreset = require('@nrwl/jest/preset').default;

module.exports = { ...nxPreset };
module.exports = { ...nxPreset, testEnvironment: 'node' };
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^17.1.0",
"@commitlint/types": "^16.2.1",
"@nrwl/cypress": "15.0.0-rc1",
"@nrwl/devkit": "15.0.0-rc1",
"@nrwl/eslint-plugin-nx": "15.0.0-rc1",
"@nrwl/jest": "15.0.0-rc1",
"@nrwl/js": "15.0.0-rc1",
"@nrwl/linter": "15.0.0-rc1",
"@nrwl/node": "15.0.0-rc1",
"@nrwl/nx-cloud": "14.7.0",
"@nrwl/nx-plugin": "15.0.0-rc1",
"@nrwl/react": "15.0.0-rc1",
"@nrwl/web": "15.0.0-rc1",
"@nrwl/workspace": "15.0.0-rc1",
"@nrwl/cypress": "15.0.1",
"@nrwl/devkit": "15.0.1",
"@nrwl/eslint-plugin-nx": "15.0.1",
"@nrwl/jest": "15.0.1",
"@nrwl/js": "15.0.1",
"@nrwl/linter": "15.0.1",
"@nrwl/node": "15.0.1",
"@nrwl/nx-cloud": "15.0.0",
"@nrwl/nx-plugin": "15.0.1",
"@nrwl/react": "15.0.1",
"@nrwl/web": "15.0.1",
"@nrwl/workspace": "15.0.1",
"@nx-plus/docusaurus": "14.0.0",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/commit-analyzer": "^9.0.2",
Expand Down Expand Up @@ -93,11 +93,12 @@
"fs-extra": "^10.1.0",
"husky": "^8.0.1",
"jest": "28.1.3",
"jest-environment-jsdom": "28.1.3",
"jsonc-eslint-parser": "^2.1.0",
"kill-port": "^1.6.1",
"lint-staged": "^13.0.3",
"node-fetch": "2.6.6",
"nx": "15.0.0-rc1",
"nx": "15.0.1",
"openapi-types": "^11.0.1",
"prettier": "2.7.1",
"react-test-renderer": "18.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('swagger-typescript generator', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace(2);
tree = createTreeWithEmptyWorkspace();
libraryGenerator(tree, { name: 'generated-ts' });
tree.write('swagger.json', MOCK_SWAGGER_JSON);
});
Expand Down
9 changes: 2 additions & 7 deletions packages/core/src/generators/utils/generate-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { readProjectConfiguration, Tree, writeJson } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

import { resolve } from 'path';

import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';

import { NxDotnetProjectGeneratorSchema } from '../../models';
Expand All @@ -25,7 +23,7 @@ describe('nx-dotnet project generator', () => {
let options: NxDotnetProjectGeneratorSchema;

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace(2);
appTree = createTreeWithEmptyWorkspace();
dotnetClient = new DotNetClient(mockDotnetFactory());

const packageJson = { scripts: {}, devDependencies: {} };
Expand Down Expand Up @@ -86,10 +84,7 @@ describe('nx-dotnet project generator', () => {
const outputPath = config.targets?.build.outputs || [];
expect(outputPath.length).toBe(1);

const absoluteDistPath = resolve(appTree.root, outputPath[0]);
const expectedDistPath = resolve(appTree.root, './dist/apps/test');

expect(absoluteDistPath).toEqual(expectedDistPath);
expect(outputPath[0]).toEqual('{workspaceRoot}/dist/apps/test');
});

it('should include serve target for applications', async () => {
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/generators/utils/generate-test-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

import * as fs from 'fs';
import { resolve } from 'path';

import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
import * as utils from '@nx-dotnet/utils';
Expand Down Expand Up @@ -100,15 +99,9 @@ describe('nx-dotnet test project generator', () => {
const config = readProjectConfiguration(appTree, testProjectName);
console.log(config.targets?.build.options);
const outputPath = config.targets?.build.outputs?.[0];
expect(outputPath).toBeTruthy();

const absoluteDistPath = resolve(appTree.root, outputPath || '');
const expectedDistPath = resolve(
appTree.root,
'./dist/apps/domain/existing-app-test',
expect(outputPath).toEqual(
'{workspaceRoot}/dist/apps/domain/existing-app-test',
);

expect(absoluteDistPath).toEqual(expectedDistPath);
});

it('should include lint target', async () => {
Expand Down
21 changes: 17 additions & 4 deletions packages/core/src/generators/utils/get-path-to-startup-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,49 @@ import {
joinPathFragments,
ProjectConfiguration,
TargetConfiguration,
workspaceRoot,
} from '@nrwl/devkit';

import { execSync } from 'child_process';
import { sync } from 'fast-glob';
import { existsSync } from 'fs';
import { basename } from 'path';
import { basename, relative, resolve } from 'path';

export function buildStartupAssemblyPath(
projectName: string,
project: ProjectConfiguration,
csProjFilePath: string,
) {
const [target, configuration] = findBuildTarget(project);
const outputDirectory = configuration?.outputs?.[0];
let outputDirectory = configuration?.outputs?.[0];
if (!outputDirectory) {
throw new Error(`Specify the output directory for ${project.root}
To generate swagger with Nx, the outputs must be captured. This is also necessary for Nx's caching mechanism.
Simply update the outputs array in project.json with the location of your build artifacts.
`);
}
outputDirectory = resolve(
outputDirectory
.replace('{workspaceRoot}', workspaceRoot)
.replace('{projectRoot}', project.root),
);
if (!existsSync(outputDirectory)) {
execSync(`${getPackageManagerCommand().exec} nx ${target} ${projectName}`);
}
console.log('before dll name');
const dllName = basename(csProjFilePath).replace(
/(?:\.csproj|\.vbproj|\.fsproj)$/,
'.dll',
);
console.log('dll name', dllName, outputDirectory);
const foundDll = sync(`**/${dllName}`, { cwd: outputDirectory })[0];
if (!foundDll) {
throw new Error(
`[nx-dotnet] Unable to locate ${dllName} in ${relative(
workspaceRoot,
outputDirectory,
)}`,
);
}
return joinPathFragments(
outputDirectory,
sync(`**/${dllName}`, { cwd: outputDirectory })[0],
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/graph/infer-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import * as config from '@nx-dotnet/utils/src/lib/utility-functions/config';
import { registerProjectTargets } from './infer-project';

describe('infer-project', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('should obey inferProjectTargets: false', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
nugetPackages: {},
Expand All @@ -15,14 +19,13 @@ describe('infer-project', () => {
expect(registerProjectTargets('libs/api/my.csproj')).toEqual({});
});

it('should generate build, lint, serve targets for projects', async () => {
it('should generate build, lint, serve targets for projects', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
nugetPackages: {},
});
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');

const targets = registerProjectTargets('libs/api/my.csproj');
console.log(targets);
expect(targets.build).toMatchInlineSnapshot(`
Object {
"configurations": Object {
Expand All @@ -36,7 +39,7 @@ describe('infer-project', () => {
"noDependencies": true,
},
"outputs": Array [
"dist/libs/api",
"{workspaceRoot}/dist/libs/api",
],
}
`);
Expand All @@ -61,7 +64,7 @@ describe('infer-project', () => {
expect(targets.test).not.toBeDefined();
});

it('should generate test target for test projects', async () => {
it('should generate test target for test projects', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
nugetPackages: {},
});
Expand All @@ -70,7 +73,6 @@ describe('infer-project', () => {
.mockReturnValueOnce('<project ref=Microsoft.NET.Test.Sdk></project>');

const targets = registerProjectTargets('libs/api/my.csproj');
console.log(targets);
expect(targets.test).toMatchInlineSnapshot(`
Object {
"executor": "@nx-dotnet/core:test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('remove-output-option', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace(2);
tree = createTreeWithEmptyWorkspace();
});

it('should not update projects where output != OutputPath', async () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/models/build-executor-configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GetBuildExecutorConfiguration } from './build-executor-configuration';

const mockPackageJson = {
version: '14.0.0',
};

jest.mock('nx/package.json', () => mockPackageJson);

describe('build executor configuration', () => {
it('should start with {workspaceRoot} for v15+', () => {
mockPackageJson.version = '15.0.0';
expect(GetBuildExecutorConfiguration('test')?.outputs?.[0]).toEqual(
'{workspaceRoot}/dist/test',
);
});

it('should not start with {workspaceRoot} for v14', () => {
mockPackageJson.version = '14.0.0';
expect(GetBuildExecutorConfiguration('test')?.outputs?.[0]).toEqual(
'dist/test',
);
});
});
Loading

0 comments on commit 90b3aab

Please sign in to comment.