Skip to content

fix: optimize workspace package resolution in dependency tree #8958

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 8 commits into from
Mar 15, 2025

Conversation

beyondkmp
Copy link
Collaborator

@beyondkmp beyondkmp commented Mar 14, 2025

fix #8956

Root Cause
The module in the workspace uses scoped naming convention like @packages/app, while the root directory is ended with packages/app. This mismatch results in returning empty and consequently leads to no dependencies being found.

for (const [key, value] of Object.entries(tree.dependencies)) {
if (this.rootDir.endsWith(path.normalize(key))) {
return value

How to fix
Read the name from package.json in the root directory and compare it with the keys in dependencies, this way it can match even with arbitrary scoped names (@xxx/yyyy).

Copy link

changeset-bot bot commented Mar 14, 2025

🦋 Changeset detected

Latest commit: 1998b64

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
app-builder-lib Patch
dmg-builder Patch
electron-builder-squirrel-windows Patch
electron-builder Patch
electron-forge-maker-appimage Patch
electron-forge-maker-nsis-web Patch
electron-forge-maker-nsis Patch
electron-forge-maker-snap Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@beyondkmp beyondkmp marked this pull request as draft March 14, 2025 05:10
@beyondkmp beyondkmp marked this pull request as ready for review March 14, 2025 06:35
@beyondkmp beyondkmp requested a review from mmaietta March 14, 2025 06:35
@mmaietta
Copy link
Collaborator

mmaietta commented Mar 14, 2025

Do we need to add a new fixture or at minimum, not commit the node_modules to the repo? Is it possible to use an existing fixture? Seems like we could simply use

await modifyPackageJson(projectDir, data => {
  data.dependencies = {
     "is-odd": "^3.0.1"
  }
}),

With this to save the lockfile

isInstallDepsBefore: true
@beyondkmp
Copy link
Collaborator Author

beyondkmp commented Mar 15, 2025

 test("yarn workspace for scope name", ({ expect }) =>
  assertPack(
    expect,
    "test-app-yarn-several-workspace",
    {
      targets: linuxDirTarget,
      projectDir: "packages/test-app",
    },
    {
      isInstallDepsBefore: true,
      projectDirCreated: projectDir => {
          return Promise.all([
            modifyPackageJson(projectDir, data => {
              data.name = "@scope/xxx-app"
              data.dependencies = {
                "is-odd": "3.0.1"
              }
            }),
          ])
        },

      packed: context => verifyAsarFileTree(expect, context.getResources(Platform.LINUX)),
    }
  ))

The modification of package.json is incorrectly targeting the project's root package.json instead of the one in packages/test-app directory. This appears to be a bug.

@beyondkmp
Copy link
Collaborator Author

beyondkmp commented Mar 15, 2025

@mmaietta I concatenated it manually. The unit test issues have been resolved. Please help review it when you have time.

test("yarn workspace for scope name", ({ expect }) =>
  assertPack(
    expect,
    "test-app-yarn-several-workspace",
    {
      targets: linuxDirTarget,
      projectDir: "packages/test-app",
    },
    {
      isInstallDepsBefore: true,
      projectDirCreated: projectDir => {
            let subAppDir = path.join(projectDir , "packages","test-app")
            return modifyPackageJson(subAppDir, data => {
              data.name = "@scope/xxx-app"
              data.dependencies = {
                "is-odd": "3.0.1"
              }
            })
        },
      packed: context => verifyAsarFileTree(expect, context.getResources(Platform.LINUX)),
    }
  ))
@mmaietta
Copy link
Collaborator

The modification of package.json is incorrectly targeting the project's root package.json instead of the one in packages/test-app directory. This appears to be a bug.

Not a bug, it's designed to work with both one-package.json and two-package.json setups but with the projectDir expected to be under app and not test-app (so part of the test-app) fixture. To flip to two-package.json "mode", it's simply setting the optional isApp boolean

export async function modifyPackageJson(projectDir: string, task: (data: any) => void, isApp = false): Promise<any> {
const file = isApp ? path.join(projectDir, "app", "package.json") : path.join(projectDir, "package.json")

Much cleaner diff now though, thank you for making the modifications!

@beyondkmp beyondkmp merged commit 81e0c47 into electron-userland:master Mar 15, 2025
27 of 28 checks passed
@Lemonexe
Copy link
Contributor

Thank you, @beyondkmp 🥇
With app-builder-lib built from master, I confirmed it to be working both in the minimal repro, as well as in our slightly more complex production app ✔️ 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants