ts-to-zod icon indicating copy to clipboard operation
ts-to-zod copied to clipboard

Types validation are not working on Ubuntu

Open kladnik opened this issue 3 years ago • 17 comments

Bug description

When ever I try to generate types I get an error. Zod and ts-to-zod are both installed. What am I doing wrong?

Input

➜  git:(develop) ✗ npx ts-to-zod ./src/types/client.ts ./src/types/client.ts
✖ Validating generated types
 ›   Error: File '/.../source.ts' is not a module.

Versions

  • Typescript: v4.4.3
  • Zod: v3.9.1

kladnik avatar Sep 27 '21 13:09 kladnik

Strange 🤔

Do you have allowSyntheticDefaultImports: true in your tsconfig.json ? (the only thing I can thing of ^^)

Edit: I tried, this is also working… (worst case you can add --skipValidation but I still want to understand…)

fabien0102 avatar Sep 27 '21 15:09 fabien0102

What version of nodejs do you have?

fabien0102 avatar Sep 27 '21 15:09 fabien0102

Do you have allowSyntheticDefaultImports: true in your tsconfig.json ? (the only thing I can thing of ^^)

No, I tried adding both true and false, but still the same issue.

Edit: I tried, this is also working… (worst case you can add --skipValidation but I still want to understand…)

It works with this, but it still should work without it.

What version of nodejs do you have?

Node version 14.17.7

kladnik avatar Sep 27 '21 15:09 kladnik

image

I can repro in codesanbox, this is maybe a nodejs issue (I’m on 12.22.6 locally)

fabien0102 avatar Sep 27 '21 15:09 fabien0102

So… probably somewhere here -> https://github.com/fabien0102/ts-to-zod/blob/1e13bbbd6daa608b43ed4bd5209d3cd723001fe3/src/core/validateGeneratedTypes.ts#L112-L114

Can you add some dirty console.log in node_modules to see the value of process.cwd and join(…) ?

fabien0102 avatar Sep 27 '21 15:09 fabien0102

Can you add some dirty console.log in node_modules to see the value of process.cwd and join(…) ?

I replaced the directory (which is the correct root folder) and this is the response:

function getPath(file) {
    const x = path_1.join(process.cwd(), file.relativePath)
    console.log(process.cwd(), x)
    return x;
}
--------------------------------------
/DIR /DIR/source.zod.ts
/DIR /DIR/source.integration.ts
/DIR /DIR/source.ts
/DIR /DIR/source.zod.ts
/DIR /DIR/source.integration.ts
/DIR /DIR/source.integration.ts
/DIR /DIR/source.integration.ts

None of these files exists

kladnik avatar Sep 27 '21 15:09 kladnik

They do exists, in a virtual file system 😅 (we are creating those files to check if the types are correct but without writing any file)

Anyway, I can repro on codesandbox… this is very strange and unexpected, I actually tried with node 14 locally, and this is working, on which operating system are you?

fabien0102 avatar Sep 27 '21 16:09 fabien0102

Hmm, this is weird then. I am using Ubuntu 21.04.

kladnik avatar Sep 27 '21 16:09 kladnik

I’m on windows, I tried on a WSL (Ubuntu 21 like you) and this is working, but codesandbox seams to have the same issue 😅

I guess this will be good enough for debugging, even if playing with vim to hack node_module is not the best DX 😓

Thanks for the reporting anyway, and sorry for this really unexpected issue, I will try to find a fix quickly (you can also give a try if you have some idea)

fabien0102 avatar Sep 27 '21 16:09 fabien0102

Thanks for looking into it so quickly. I'm okay for now running it with --skipValidation for now, so just take your time.

kladnik avatar Sep 27 '21 16:09 kladnik

Normally the library is well tested, this is just to help me to find some little edge cases 😉

fabien0102 avatar Sep 27 '21 16:09 fabien0102

Hi, Just to let you know, I'm seeing this on MacOS, node v14.17.6.

I think we have the major desktop OSs covered :)

brendony avatar Oct 07 '21 04:10 brendony

And I'm seeing the same in PowerShell and cmd on Windows. I also see it when running PS or cmd as admin.

(node v14.17.6)

... good work and awesome tool btw 👍

ehjelholt avatar Oct 13 '21 13:10 ehjelholt

I’m currently working on #42 (some edge cases are very challenging 😅) and I’m having a look on this one (also happy if somebody open an amazing PR that fix this 😇)

Thanks for the reporting!

fabien0102 avatar Oct 13 '21 14:10 fabien0102

I'm able to get ts-to-zod working in one of two scenarios.

  1. package.json has "type": "module"

tsconfig must have "module": "es2020" but should also have "moduleResolution": "node" and "esModuleInterop": true

  1. package.json does not have "type": "module"

tsconfig must have "module": "commonJS"

If you don't have the right combination of settings, you get the same error reported by @kladnik when trying to run ts-to-zod

HartS avatar Sep 01 '22 07:09 HartS

Any workaround for this issue other than skipValidation?

johanneshayry avatar Oct 31 '22 11:10 johanneshayry

I'm having the same bug on ts-to-zod/2.0.1 darwin-arm64 node-v19.5.0 , with zod v3.20.2. It happens both on running the global binary (i.e. ts-to-zod in.ts out.ts) and when running npx inside a NPM project with ts-node installed

moraesvic avatar Feb 01 '23 22:02 moraesvic