Skip to content

fix(yarn-plugin-external-workspaces): fix output paths #3592

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

Merged
merged 5 commits into from
May 7, 2025
Merged
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/stupid-rats-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rnx-kit/yarn-plugin-external-workspaces": patch
---

Fixed output being saved relatively to the current working directory instead of
the project root
6 changes: 3 additions & 3 deletions .yarn/plugins/@rnx-kit/yarn-plugin-external-workspaces.cjs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import {
} from "@yarnpkg/core";
import { type PortablePath, npath, ppath } from "@yarnpkg/fslib";
import fs from "node:fs";
import path from "node:path";
import {
type PackagePaths,
type WorkspaceOutputGeneratedContent,
@@ -40,8 +41,19 @@ export const externalWorkspacesConfiguration: CustomConfiguration = {
},
};

function coercePortablePath(value: unknown): PortablePath {
return npath.toPortablePath(typeof value === "string" ? value : "");
function coercePortablePath(
value: unknown,
projectCwd: PortablePath | null
): PortablePath {
// If we have a project root, convert to an absolute path otherwise Yarn will
// use the current working directory instead.
const root = projectCwd ? npath.fromPortablePath(projectCwd) : "";

// Both `npath.join()` and `ppath.join()` mishandle Windows paths, which is
// why we convert them to strings first and use `node:path.join()` instead.
const p = path.join(root, typeof value === "string" ? value : "");

return npath.toPortablePath(p);
}

/**
@@ -53,8 +65,15 @@ export function getPluginConfiguration(configuration: Configuration): {
outputPath: PortablePath | null;
outputOnlyOnCommand: boolean;
} {
const provider = coercePortablePath(configuration.get(PROVIDER_KEY));
const outputPath = coercePortablePath(configuration.get(OUTPUT_PATH_KEY));
const { projectCwd } = configuration;
const provider = coercePortablePath(
configuration.get(PROVIDER_KEY),
projectCwd
);
const outputPath = coercePortablePath(
configuration.get(OUTPUT_PATH_KEY),
projectCwd
);
const outputOnlyOnCommand = Boolean(configuration.get(OUTPUT_ON_COMMAND_KEY));
return { provider, outputPath, outputOnlyOnCommand };
}
2 changes: 1 addition & 1 deletion incubator/yarn-plugin-external-workspaces/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import {
type Resolver,
} from "@yarnpkg/core";
import { npath } from "@yarnpkg/fslib";
import { getPluginConfiguration } from "./cofiguration";
import { getPluginConfiguration } from "./configuration";
import { outputWorkspaces } from "./outputCommand";
import { getWorkspaceTracker } from "./tracker";

2 changes: 1 addition & 1 deletion incubator/yarn-plugin-external-workspaces/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Plugin } from "@yarnpkg/core";
import { externalWorkspacesConfiguration } from "./cofiguration";
import { externalWorkspacesConfiguration } from "./configuration";
import { ExternalWorkspaceFetcher } from "./fetcher";
import { afterAllInstalled, reduceDependency } from "./hooks";
import { OutputWorkspaces } from "./outputCommand";
13 changes: 9 additions & 4 deletions incubator/yarn-plugin-external-workspaces/src/outputCommand.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { Configuration, Project, structUtils } from "@yarnpkg/core";
import { npath, type PortablePath, ppath } from "@yarnpkg/fslib";
import { Command, Option, UsageError } from "clipanion";
import fs from "node:fs";
import { getPluginConfiguration } from "./cofiguration";
import { getPluginConfiguration } from "./configuration";
import { type WorkspaceOutputGeneratedContent } from "./types";

const outputVersion = "1.0.0";
@@ -96,15 +96,17 @@ export function outputWorkspaces(
): void {
// ensure we have a valid filename and that the directory exists
const includesJson = outputPath.endsWith(".json");
const outputDir = includesJson ? ppath.dirname(outputPath) : outputPath;
const outputDir = npath.fromPortablePath(
includesJson ? ppath.dirname(outputPath) : outputPath
);
const outputFile = includesJson
? ppath.basename(outputPath)
: fallbackOutputFilename(project);

if (!checkOnly && !fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true, mode: 0o755 });
}
const fullPath = npath.join(npath.fromPortablePath(outputDir), outputFile);
const fullPath = npath.join(outputDir, outputFile);

const workspaces: Record<string, string> = {};
// iterate the workspaces and record their names and paths
@@ -117,7 +119,10 @@ export function outputWorkspaces(
});

// grab the relative path from the config to the repo root
const repoPath = ppath.relative(outputDir, project.cwd);
const repoPath = npath.relative(
outputDir,
npath.fromPortablePath(project.cwd)
);

// create the new generated content
const generated: WorkspaceOutputGeneratedContent = {
2 changes: 1 addition & 1 deletion incubator/yarn-plugin-external-workspaces/src/tracker.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import {
getFinderFromJsConfig,
getFinderFromJsonConfig,
getPluginConfiguration,
} from "./cofiguration";
} from "./configuration";
import { type PackagePaths } from "./types";
import { ExternalWorkspace } from "./workspace";

Loading
Oops, something went wrong.