Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/lib/lockfile/helpers/generate-pnpm-lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export async function generatePnpmLockfile({

return [
".",
pnpmMapImporter(importer, {
pnpmMapImporter(".", importer, {
includeDevDependencies,
includePatchedDependencies,
directoryByPackageName,
Expand All @@ -130,7 +130,7 @@ export async function generatePnpmLockfile({

return [
importerId,
pnpmMapImporter(importer, {
pnpmMapImporter(importerId, importer, {
includeDevDependencies,
includePatchedDependencies,
directoryByPackageName,
Expand Down
32 changes: 27 additions & 5 deletions src/lib/lockfile/helpers/pnpm-map-importer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import type {
ProjectSnapshot,
ResolvedDependencies,
Expand All @@ -7,6 +8,7 @@ import { mapValues } from "remeda";

/** Convert dependency links */
export function pnpmMapImporter(
importerPath: string,
{ dependencies, devDependencies, ...rest }: ProjectSnapshot,
{
includeDevDependencies,
Expand All @@ -19,21 +21,41 @@ export function pnpmMapImporter(
): ProjectSnapshot {
return {
dependencies: dependencies
? pnpmMapDependenciesLinks(dependencies, directoryByPackageName)
? pnpmMapDependenciesLinks(
importerPath,
dependencies,
directoryByPackageName
)
: undefined,
devDependencies:
includeDevDependencies && devDependencies
? pnpmMapDependenciesLinks(devDependencies, directoryByPackageName)
? pnpmMapDependenciesLinks(
importerPath,
devDependencies,
directoryByPackageName
)
: undefined,
...rest,
};
}

function pnpmMapDependenciesLinks(
importerPath: string,
def: ResolvedDependencies,
directoryByPackageName: { [packageName: string]: string }
): ResolvedDependencies {
return mapValues(def, (value, key) =>
value.startsWith("link:") ? `link:./${directoryByPackageName[key]}` : value
);
return mapValues(def, (value, key) => {
if (value.startsWith("link:")) {
let relativePath = path.relative(
importerPath,
directoryByPackageName[key]
);
if (!relativePath.startsWith(".") && !relativePath.startsWith("/")) {
relativePath = `./${relativePath}`;
}
Comment on lines -36 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because directoryByPackageName[key] is a path relative to the project root, I think we need to add a logic to resolve a relative path from the importer's path.

Copy link
Owner

@0x80 0x80 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why this check would be necessary:

if (!relativePath.startsWith(".") && !relativePath.startsWith("/"))
  1. In what scenario does path.relative() return a valid path that is not starting with "."?
  2. What valid relative path would start with a "/"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. path.relative formats the path like "dir1/dir2" instead of "./dir1/dir2". I saw pnpm-lock.yaml file has all path starting with "./" so I added this line.
  2. Good point! I can't think of any case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it and it seems to work for me. If you can simplify the check I can merge this. I suggest to change the function to:

function pnpmMapDependenciesLinks(
  importerPath: string,
  def: ResolvedDependencies,
  directoryByPackageName: { [packageName: string]: string }
): ResolvedDependencies {
  return mapValues(def, (value, key) => {
    if (!value.startsWith("link:")) {
      return value;
    }

    const relativePath = path.relative(
      importerPath,
      directoryByPackageName[key]
    );

    return relativePath.startsWith(".")
      ? `link:${relativePath}`
      : `link:./${relativePath}`;
  });
}

return `link:${relativePath}`;
} else {
return value;
}
});
}