Skip to content
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

pnpm catalog fails to parse version in package.json #1965

Open
yutakobayashidev opened this issue Mar 10, 2025 · 9 comments
Open

pnpm catalog fails to parse version in package.json #1965

yutakobayashidev opened this issue Mar 10, 2025 · 9 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@yutakobayashidev
Copy link

When using Orval with the catalog feature of pnpm workspace, an error occurs because it cannot parse the version in package.json.

{
  "name": "test",
  "dependencies": {
    "@tanstack/react-query": "catalog:"
  }
}
packages:
  - packages/*

catalog:
  "@tanstack/react-query": "^5.67.2"
~/pnpm-test/packages/test$ pnpm orval
🍻 Start orval v7.6.0 - A swagger client generator for typescript
🛑 petstore - Error: Invalid argument not valid semver ('catalog:' received)

I tried to modify the code so that it parses the pnpm-workspace.yaml, extracts the catalog version, and maps it to package.json as follows. What do you think? There might be adjustments needed, like handling cases where a name is not found in the catalog and retaining the original 'catalog:'. If everything looks good, I can create a pull request.

interface PnpmWorkspace {
  packages?: string[];
  catalog?: Record<string, string>;
}

const loadPnpmWorkspace = async (
  workspace = process.cwd(),
): Promise<PnpmWorkspace | undefined> => {
  const workspacePath = await findUp(['pnpm-workspace.yaml'], {
    cwd: workspace,
  });

  if (!workspacePath) {
    return undefined;
  }

  try {
    const content = await fs.readFile(workspacePath, 'utf8');
    return yaml.load(content) as PnpmWorkspace;
  } catch {
    return;
  }
};

const replaceCatalogReferences = (
  pkg: PackageJson,
  catalog: Record<string, string>,
): PackageJson => {
  const replaceDependencies = (
    deps?: Record<string, string>,
  ): Record<string, string> | undefined => {
    if (!deps) return undefined;

    const newDeps: Record<string, string> = {};
    for (const [name, version] of Object.entries(deps)) {
      if (typeof version === 'string' && version === 'catalog:') {
        newDeps[name] = catalog[name] || version;
      } else {
        newDeps[name] = version;
      }
    }
    return newDeps;
  };

  return {
    ...pkg,
    dependencies: replaceDependencies(pkg.dependencies),
    devDependencies: replaceDependencies(pkg.devDependencies),
    peerDependencies: replaceDependencies(pkg.peerDependencies),
  };
};

export const loadPackageJson = async (
  packageJson?: string,
  workspace = process.cwd(),
): Promise<PackageJson | undefined> => {

  const pkgPath = packageJson
    ? normalizePath(packageJson, workspace)
    : await findUp(['package.json'], { cwd: workspace });

  if (!pkgPath || !fs.existsSync(pkgPath)) {
    return;
  }

  const pkg = await import(pkgPath);

  const pnpmWorkspace = await loadPnpmWorkspace(workspace);
  const catalog = pnpmWorkspace?.catalog;

  if (!catalog) {
    return pkg;
  }

  return replaceCatalogReferences(pkg, catalog);
};
@yutakobayashidev
Copy link
Author

duplicate #1931

@yutakobayashidev
Copy link
Author

yutakobayashidev commented Mar 10, 2025

I used version 7.6.0, but the issue persists. The catalog is used in multiple dependencies.

🛑 petstore - Oups... 🍻. An Error occurred while writing file => Error: Invalid argument not valid semver ('catalog:' received)

@melloware
Copy link
Collaborator

@shotanue can you confirm?

@melloware melloware added the bug Something isn't working label Mar 10, 2025
@shotanue
Copy link

7.6.0 seems to have fixed the issue I reported in #1931.
@melloware, Thank you for addressing it.

Here's my orval.config.ts that I'm using:

import { defineConfig } from "orval";
export default defineConfig({
  xxxxx: {
    input: "xxxxx/openapi.yaml",
    output: {
      mode: "split",
      client: "react-query",
      target: "xxxxxx",
      override: {
        query: {
          useSuspenseQuery: true,
+          version: 5,
        },
        mutator: {
          path: "./src/custom-instance.ts",
          name: "customInstance",
        },
      },
    },
  },
});

When using pnpm catalogs, it appears that specifying override.query.version = 5 is necessary.
I tried removing this option in 7.6.0, and it resulted in a "not valid semver" error.
It functions as a workaround for errors related to the pnpm catalogs protocol.
This may differ from the option's original purpose, though. 🤔

@melloware melloware added this to the 7.6.0 milestone Mar 10, 2025
@melloware melloware added the workaround A workaround has been provided label Mar 10, 2025
@yutakobayashidev
Copy link
Author

yutakobayashidev commented Mar 10, 2025

I tried that workaround, but when there are other dependencies managed by the catalog, a slightly different error occurs. In #1931, it was not an error related to file writing. The issue is resolved and works fine by applying a patch that resolves the catalog version mentioned in the first comment.

🛑 petstore - Oups... 🍻. An Error occurred while writing file => Error: Invalid argument not valid semver ('catalog:' received)

@melloware
Copy link
Collaborator

@shotanue any thoughts?

@shotanue
Copy link

I apologize for not reading the issue body thoroughly.
This issue was actually a proposal for catalog support.

I did not come up with alternative workarounds.

As an Orval user, it would be better if Orval supported pnpm catalogs due to the current limitations that sometimes require workarounds, which don't always work reliably.

The proposal to support pnpm catalogs with sanitizing package.json is a good idea 👍 If it supported https://pnpm.io/catalogs#named-catalogs, it would make for an even better implementation.

The proposed solution could be applicable beyond react-query, eliminating the need for workarounds, which would be a great improvement for Orval.

@melloware melloware reopened this Mar 11, 2025
@melloware
Copy link
Collaborator

Reopened. I know nothing about PNPM and catalog so this will most likely require a PR from a user...

@melloware melloware added enhancement New feature or request and removed bug Something isn't working workaround A workaround has been provided labels Mar 11, 2025
@arthurfiorette
Copy link
Contributor

Puting packageJson: 'fake', in options supresses this issue.suppresses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants