Skip to content

Commit

Permalink
feat(core): import warns when source and destination directories are …
Browse files Browse the repository at this point in the history
…different (#27875)

This PR adds a warning when the user choose different source and
destination roots. This is a problem for Nx workspaces using path
options in `project.json`, and possibly other config files such as
`tsconfig.json`, `jest.config.ts`, etc.

Note: Also included a guard that the destination directory isn't an
absolute path like `/tmp/foo`, because the behavior will not work as
expected.

The message when the source is an Nx workspace:
<img width="1392" alt="Screenshot 2024-09-11 at 9 32 54 AM"
src="https://github.com/user-attachments/assets/c8ebedba-fd66-4dbf-ada9-eacf86bd67fc">

The message when the source is not an Nx workspace:
<img width="1392" alt="Screenshot 2024-09-11 at 9 30 44 AM"
src="https://github.com/user-attachments/assets/ade9fbd1-4d5d-4d0c-93f6-eaad176af333">

(cherry picked from commit 8b177bd)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Sep 12, 2024
1 parent a3fef98 commit 9352f83
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/nx/src/command-line/import/import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, join, relative, resolve } from 'path';
import { dirname, isAbsolute, join, relative, resolve } from 'path';
import { minimatch } from 'minimatch';
import { existsSync, promises as fsp } from 'node:fs';
import * as chalk from 'chalk';
Expand Down Expand Up @@ -177,6 +177,13 @@ export async function importHandler(options: ImportOptions) {
}

const absSource = join(sourceTempRepoPath, source);

if (isAbsolute(destination)) {
throw new Error(
`The destination directory must be a relative path in this repository.`
);
}

const absDestination = join(process.cwd(), destination);

const destinationGitClient = new GitRepository(process.cwd());
Expand Down Expand Up @@ -210,6 +217,8 @@ export async function importHandler(options: ImportOptions) {
packageManager
);

const sourceIsNxWorkspace = existsSync(join(sourceGitClient.root, 'nx.json'));

const relativeDestination = relative(
destinationGitClient.root,
absDestination
Expand Down Expand Up @@ -310,6 +319,19 @@ export async function importHandler(options: ImportOptions) {

await warnOnMissingWorkspacesEntry(packageManager, pmc, relativeDestination);

if (source != destination) {
output.warn({
title: `Check configuration files`,
bodyLines: [
`The source directory (${source}) and destination directory (${destination}) are different.`,
`You may need to update configuration files to match the directory in this repository.`,
sourceIsNxWorkspace
? `For example, path options in project.json such as "main", "tsConfig", and "outputPath" need to be updated.`
: `For example, relative paths in tsconfig.json and other tooling configuration files may need to be updated.`,
],
});
}

// When only a subdirectory is imported, there might be devDependencies in the root package.json file
// that needs to be ported over as well.
if (ref) {
Expand Down

0 comments on commit 9352f83

Please sign in to comment.