Skip to content

Commit

Permalink
fix(core): switch from appRootPath to workspaceRoot (#9600)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 30, 2022
1 parent b952d0c commit 3b21f4d
Show file tree
Hide file tree
Showing 66 changed files with 189 additions and 194 deletions.
6 changes: 3 additions & 3 deletions packages/angular/src/executors/utilities/tailwindcss.ts
@@ -1,5 +1,5 @@
import { logger } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
import { existsSync } from 'fs';
import { join, relative } from 'path';
import * as postcssImport from 'postcss-import';
Expand Down Expand Up @@ -27,7 +27,7 @@ export function getTailwindSetup(
if (!tailwindConfigPath) {
// Try to find TailwindCSS configuration file in the project or workspace root.
const tailwindConfigFile = 'tailwind.config.js';
for (const path of [basePath, appRootPath]) {
for (const path of [basePath, workspaceRoot]) {
const fullPath = join(path, tailwindConfigFile);
if (existsSync(fullPath)) {
tailwindConfigPath = fullPath;
Expand All @@ -46,7 +46,7 @@ export function getTailwindSetup(
tailwindPackagePath = require.resolve('tailwindcss');
} catch {
const relativeTailwindConfigPath = relative(
appRootPath,
workspaceRoot,
tailwindConfigPath
);
logger.warn(
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/generators/move/lib/update-ng-package.ts
@@ -1,5 +1,5 @@
import { readProjectConfiguration, Tree, updateJson } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
import { getNewProjectName } from '@nrwl/workspace/src/generators/move/lib/utils';
import { join, relative } from 'path';
import { Schema } from '../schema';
Expand All @@ -17,7 +17,7 @@ export function updateNgPackage(tree: Tree, schema: Schema): void {
return;
}

const rootOffset = relative(join(appRootPath, project.root), appRootPath);
const rootOffset = relative(join(workspaceRoot, project.root), workspaceRoot);
let output = `dist/${project.root}`;
if (project.targets?.build?.outputs?.length > 0) {
output = project.targets.build.outputs[0];
Expand Down
Expand Up @@ -45,7 +45,7 @@ exports[`opt-out-testbed-teardown migration update configureTestingModule in tes
declarations: [AppComponent],
teardown: { destroyAfterEach: false }
}).compileComponents();
expect(true).toBe(true);
});
});
Expand Down Expand Up @@ -121,7 +121,7 @@ exports[`opt-out-testbed-teardown migration update configureTestingModule in tes
declarations: [AppComponent],
teardown: { destroyAfterEach: false }
}).compileComponents();
expect(true).toBe(true);
});
});
Expand Down
Expand Up @@ -7,7 +7,7 @@ describe('opt-out-testbed-teardown migration', () => {

beforeEach(() => {
tree = createTreeWithEmptyWorkspace(2);
jest.doMock('@nrwl/devkit', () => ({ appRootPath: '' }));
jest.doMock('@nrwl/devkit', () => ({ workspaceRoot: '' }));
});

it('should warn when the jestConfig property is not configured', async () => {
Expand Down Expand Up @@ -668,7 +668,7 @@ describe('opt-out-testbed-teardown migration', () => {
await TestBed.configureTestingModule({
declarations: [AppComponent],
}).compileComponents();
expect(true).toBe(true);
});
});
Expand Down Expand Up @@ -728,7 +728,7 @@ describe('opt-out-testbed-teardown migration', () => {
await TestBed.configureTestingModule({
declarations: [AppComponent],
}).compileComponents();
expect(true).toBe(true);
});
});
Expand Down
8 changes: 5 additions & 3 deletions packages/angular/src/utils/mfe/mfe-webpack.ts
@@ -1,6 +1,6 @@
import { existsSync, readFileSync } from 'fs';
import { NormalModuleReplacementPlugin } from 'webpack';
import { normalizePath, joinPathFragments, appRootPath } from '@nrwl/devkit';
import { normalizePath, joinPathFragments, workspaceRoot } from '@nrwl/devkit';
import { dirname } from 'path';
import { ParsedCommandLine } from 'typescript';
import {
Expand Down Expand Up @@ -40,7 +40,9 @@ export function shareWorkspaceLibraries(
const pathMappings: { name: string; path: string }[] = [];
for (const [key, paths] of Object.entries(tsconfigPathAliases)) {
if (libraries && libraries.includes(key)) {
const pathToLib = normalizePath(joinPathFragments(appRootPath, paths[0]));
const pathToLib = normalizePath(
joinPathFragments(workspaceRoot, paths[0])
);
pathMappings.push({
name: key,
path: pathToLib,
Expand Down Expand Up @@ -84,7 +86,7 @@ export function shareWorkspaceLibraries(
export function sharePackages(
packages: string[]
): Record<string, SharedLibraryConfig> {
const pkgJsonPath = joinPathFragments(appRootPath, 'package.json');
const pkgJsonPath = joinPathFragments(workspaceRoot, 'package.json');
if (!existsSync(pkgJsonPath)) {
throw new Error(
'NX MFE: Could not find root package.json to determine dependency versions.'
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-nx/src/configs/typescript.ts
@@ -1,4 +1,4 @@
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';

/**
* This configuration is intended to be applied to ALL .ts and .tsx files
Expand All @@ -12,7 +12,7 @@ export default {
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
tsconfigRootDir: appRootPath,
tsconfigRootDir: workspaceRoot,
},
plugins: ['@typescript-eslint'],
extends: [
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-nx/src/constants.ts
@@ -1,7 +1,7 @@
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
import { join } from 'path';

export const WORKSPACE_PLUGIN_DIR = join(appRootPath, 'tools/eslint-rules');
export const WORKSPACE_PLUGIN_DIR = join(workspaceRoot, 'tools/eslint-rules');

/**
* We add a namespace so that we mitigate the risk of rule name collisions as much as
Expand Down
Expand Up @@ -13,12 +13,10 @@ jest.mock('fs', () => require('memfs').fs);

jest.mock('@nrwl/devkit', () => ({
...jest.requireActual<any>('@nrwl/devkit'),
appRootPath: '/root',
workspaceRoot: '/root',
}));

jest.mock('nx/src/utils/app-root', () => ({
appRootPath: '/root',
workspaceRoot: '/root',
}));

Expand Down
@@ -1,5 +1,5 @@
import {
appRootPath,
workspaceRoot,
joinPathFragments,
normalizePath,
ProjectGraphExternalNode,
Expand Down Expand Up @@ -146,7 +146,7 @@ export default createESLintRule<Options, MessageIds>({
* Globally cached info about workspace
*/
const projectPath = normalizePath(
(global as any).projectPath || appRootPath
(global as any).projectPath || workspaceRoot
);
/**
* Only reuse graph when running from terminal
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-nx/src/utils/ast-utils.ts
Expand Up @@ -5,11 +5,11 @@ import { existsSync, readFileSync } from 'fs';
import { dirname } from 'path';
import ts = require('typescript');
import { logger } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';

function tryReadBaseJson() {
try {
return readJsonFile(joinPathFragments(appRootPath, 'tsconfig.base.json'));
return readJsonFile(joinPathFragments(workspaceRoot, 'tsconfig.base.json'));
} catch (e) {
logger.warn(`Error reading "tsconfig.base.json": \n${JSON.stringify(e)}`);
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/next/plugins/with-nx.ts
Expand Up @@ -4,7 +4,7 @@ import type { NextConfig } from 'next/dist/server/config';
import type { WebpackConfigOptions } from '../src/utils/types';

const { join } = require('path');
const { appRootPath } = require('@nrwl/devkit');
const { workspaceRoot } = require('@nrwl/devkit');
const { workspaceLayout } = require('@nrwl/devkit');

export interface WithNxOptions extends NextConfig {
Expand Down Expand Up @@ -44,7 +44,7 @@ function withNx(nextConfig = {} as WithNxOptions) {
*/

// Include workspace libs in css/sass loaders
const includes = [join(appRootPath, workspaceLayout().libsDir)];
const includes = [join(workspaceRoot, workspaceLayout().libsDir)];

const nextCssLoaders = config.module.rules.find(
(rule) => typeof rule.oneOf === 'object'
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/utils/generate-globs.ts
@@ -1,5 +1,5 @@
import { joinPathFragments, logger } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
import { getSourceDirOfDependentProjects } from 'nx/src/utils/project-graph-utils';
import { resolve } from 'path';

Expand All @@ -19,7 +19,7 @@ export function createGlobPatternsOfDependentProjects(
const projectDirs = getSourceDirOfDependentProjects(projectName);

return projectDirs.map((sourceDir) =>
resolve(appRootPath, joinPathFragments(sourceDir, fileGlobPattern))
resolve(workspaceRoot, joinPathFragments(sourceDir, fileGlobPattern))
);
} catch (e) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/utils/node.config.spec.ts
Expand Up @@ -5,7 +5,7 @@ import { BuildNodeBuilderOptions } from './types';

jest.mock('tsconfig-paths-webpack-plugin');
jest.mock('@nrwl/devkit', () => ({
get appRootPath() {
get workspaceRoot() {
return join(__dirname, '../../../..');
},
}));
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/utils/node.config.ts
@@ -1,4 +1,4 @@
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
import { Configuration } from 'webpack';
import { merge } from 'webpack-merge';

Expand Down Expand Up @@ -32,7 +32,7 @@ function getNodePartial(options: BuildNodeBuilderOptions) {
}

if (options.externalDependencies === 'all') {
const modulesDir = `${appRootPath}/node_modules`;
const modulesDir = `${workspaceRoot}/node_modules`;
webpackConfig.externals = [nodeExternals({ modulesDir })];
} else if (Array.isArray(options.externalDependencies)) {
webpackConfig.externals = [
Expand Down
4 changes: 2 additions & 2 deletions packages/nx-plugin/src/utils/testing-utils/nx-project.ts
@@ -1,4 +1,4 @@
import { appRootPath } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
import {
getPackageManagerCommand,
readJsonFile,
Expand Down Expand Up @@ -31,7 +31,7 @@ export function patchPackageJsonForPlugin(
) {
const path = tmpProjPath('package.json');
const json = readJsonFile(path);
json.devDependencies[npmPackageName] = `file:${appRootPath}/${distPath}`;
json.devDependencies[npmPackageName] = `file:${workspaceRoot}/${distPath}`;
writeJsonFile(path, json);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/nx/src/adapter/compat.ts
Expand Up @@ -4,7 +4,7 @@ import {
workspaceConfigName,
Workspaces,
} from '../shared/workspace';
import { appRootPath } from '../utils/app-root';
import { workspaceRoot } from '../utils/app-root';

/* eslint-disable */
const Module = require('module');
Expand All @@ -24,15 +24,15 @@ if (!patched) {
core._test_addWorkspaceFile('workspace.json', core.WorkspaceFormat.JSON);
const originalReadWorkspace = core.readWorkspace;
core.readWorkspace = (path, ...rest) => {
const configFile = workspaceConfigName(appRootPath);
const configFile = workspaceConfigName(workspaceRoot);
if (!configFile) {
path = 'workspace.json';
}
return originalReadWorkspace.apply(this, [path, ...rest]);
};
const originalWriteWorkspace = core.writeWorkspace;
core.writeWorkspace = (...args) => {
const configFile = workspaceConfigName(appRootPath);
const configFile = workspaceConfigName(workspaceRoot);
if (!loggedWriteWorkspaceWarning) {
if (configFile) {
logger.warn(
Expand Down Expand Up @@ -68,7 +68,7 @@ if (!patched) {
logger.debug(
'[NX] Angular devkit readJsonWorkspace fell back to Nx workspaces logic'
);
const w = new Workspaces(appRootPath);
const w = new Workspaces(workspaceRoot);

// Read our v1 workspace schema
const workspaceConfiguration = resolveOldFormatWithInlineProjects(
Expand Down
6 changes: 3 additions & 3 deletions packages/nx/src/command-line/dep-graph.ts
@@ -1,4 +1,4 @@
import { appRootPath } from 'nx/src/utils/app-root';
import { workspaceRoot } from 'nx/src/utils/app-root';
import { watch } from 'chokidar';
import { createHash } from 'crypto';
import { existsSync, readFileSync, statSync, writeFileSync } from 'fs';
Expand Down Expand Up @@ -211,7 +211,7 @@ export async function generateGraph(
graph = filterGraph(graph, args.focus || null, args.exclude || []);

if (args.file) {
const workspaceFolder = appRootPath;
const workspaceFolder = workspaceRoot;
const ext = extname(args.file);
const fullFilePath = isAbsolute(args.file)
? args.file
Expand Down Expand Up @@ -420,7 +420,7 @@ function getIgnoredGlobs(root: string) {
}

function startWatcher() {
createFileWatcher(appRootPath, async () => {
createFileWatcher(workspaceRoot, async () => {
output.note({ title: 'Recalculating project graph...' });

const newGraphClientResponse = await createDepGraphClientResponse();
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/format.ts
Expand Up @@ -16,7 +16,7 @@ import {
workspaceConfigName,
WorkspaceJsonConfiguration,
} from 'nx/src/shared/workspace';
import { appRootPath } from 'nx/src/utils/app-root';
import { workspaceRoot } from 'nx/src/utils/app-root';
import * as prettier from 'prettier';
import { sortObjectByKeys } from 'nx/src/utils/object-sort';
import {
Expand All @@ -43,7 +43,7 @@ export async function format(

switch (command) {
case 'write':
const workspaceJsonPath = workspaceConfigName(appRootPath);
const workspaceJsonPath = workspaceConfigName(workspaceRoot);
if (workspaceJsonPath) {
updateWorkspaceJsonToMatchFormatVersion(workspaceJsonPath);
sortWorkspaceJson(workspaceJsonPath);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/list.ts
@@ -1,4 +1,4 @@
import { appRootPath } from 'nx/src/utils/app-root';
import { workspaceRoot } from 'nx/src/utils/app-root';
import { output } from '../utils/output';
import {
fetchCommunityPlugins,
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function listHandler(args: ListArgs): Promise<void> {
});

const installedPlugins = getInstalledPluginsFromPackageJson(
appRootPath,
workspaceRoot,
corePlugins,
communityPlugins
);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/nx-commands.ts
Expand Up @@ -5,7 +5,7 @@ import * as yargs from 'yargs';
import { generateDaemonHelpOutput } from '../core/project-graph/daemon/client/generate-help-output';
import { nxVersion } from '../utils/versions';
import { examples } from './examples';
import { appRootPath } from 'nx/src/utils/app-root';
import { workspaceRoot } from 'nx/src/utils/app-root';
import { getPackageManagerCommand } from '../utils/package-manager';
import { writeJsonFile } from 'nx/src/utils/fileutils';

Expand Down Expand Up @@ -731,7 +731,7 @@ function nxCliPath() {

// Set NODE_PATH so that these modules can be used for module resolution
addToNodePath(path.join(tmpDir, 'node_modules'));
addToNodePath(path.join(appRootPath, 'node_modules'));
addToNodePath(path.join(workspaceRoot, 'node_modules'));

return path.join(tmpDir, `node_modules`, '.bin', 'nx');
} catch (e) {
Expand Down
3 changes: 0 additions & 3 deletions packages/nx/src/command-line/report.spec.ts
@@ -1,10 +1,7 @@
import { findInstalledCommunityPlugins } from './report';
// import * as devkit from '@nrwl/devkit';
import * as fileUtils from '../utils/fileutils';
import { join } from 'path';

jest.mock('nx/src/utils/app-root', () => ({
appRootPath: '',
workspaceRoot: '',
}));

Expand Down

0 comments on commit 3b21f4d

Please sign in to comment.