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

refactor: New file generates import from wrong module specifier #58897

Open
gsk143 opened this issue Jun 12, 2024 · 18 comments
Open

refactor: New file generates import from wrong module specifier #58897

gsk143 opened this issue Jun 12, 2024 · 18 comments
Assignees
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output

Comments

@gsk143
Copy link

gsk143 commented Jun 12, 2024

Does this issue occur when all extensions are disabled?: Yes

Version: 1.90.0 (user setup)
Commit: 89de5a8d4d6205e5b11647eb6a74844ca23d2573
Date: 2024-06-04T19:33:54.889Z
Electron: 29.4.0
ElectronBuildId: 9593362
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Windows_NT x64 10.0.22631

Steps to Reproduce:

import prisma from "@/prisma/client";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";

const createIssueSchema = z.object({
  title: z.string().min(1).max(255),
  description: z.string().min(1),
});

Try refactoring createIssueSchema variable to a new file
2024-06-12 09:20:14.898 [error] <semantic> TypeScript Server Error (5.5.0-dev.20240505) Debug Failure. Error: Debug Failure. at Object.addImportFromExportedSymbol (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:154711:32) at c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144661:19 at Map.forEach (<anonymous>) at addTargetFileImports (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144655:17) at getNewStatementsAndRemoveFromOldFile (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:143871:3) at doChange4 (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144853:3) at c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144842:77 at _ChangeTracker.with (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:174963:5) at Object.getRefactorEditsToMoveToNewFile [as getEditsForAction] (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144842:60) at Object.getEditsForRefactor (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:142944:31) at Object.getEditsForRefactor2 [as getEditsForRefactor] (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:150350:32) at IpcIOSession.getEditsForRefactor (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:191692:49) at getEditsForRefactor (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:189914:43) at c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192078:69 at IpcIOSession.executeWithRequestId (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192070:14) at IpcIOSession.executeCommand (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192078:29) at IpcIOSession.onMessage (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192120:51) at process.<anonymous> (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\tsserver.js:528:14) at process.emit (node:events:514:28) at emit (node:internal/child_process:951:14) at process.processTicksAndRejections (node:internal/process/task_queues:83:21): Error: <semantic> TypeScript Server Error (5.5.0-dev.20240505) Debug Failure. Error: Debug Failure. at Object.addImportFromExportedSymbol (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:154711:32) at c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144661:19 at Map.forEach (<anonymous>) at addTargetFileImports (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144655:17) at getNewStatementsAndRemoveFromOldFile (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:143871:3) at doChange4 (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144853:3) at c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144842:77 at _ChangeTracker.with (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:174963:5) at Object.getRefactorEditsToMoveToNewFile [as getEditsForAction] (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:144842:60) at Object.getEditsForRefactor (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:142944:31) at Object.getEditsForRefactor2 [as getEditsForRefactor] (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:150350:32) at IpcIOSession.getEditsForRefactor (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:191692:49) at getEditsForRefactor (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:189914:43) at c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192078:69 at IpcIOSession.executeWithRequestId (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192070:14) at IpcIOSession.executeCommand (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192078:29) at IpcIOSession.onMessage (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\typescript.js:192120:51) at process.<anonymous> (c:\Users\ASUS\.vscode\extensions\ms-vscode.vscode-typescript-next-5.5.20240505\node_modules\typescript\lib\tsserver.js:528:14) at process.emit (node:events:514:28) at emit (node:internal/child_process:951:14) at process.processTicksAndRejections (node:internal/process/task_queues:83:21) at n.create (c:\Users\ASUS\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\typescript-language-features\dist\extension.js:1:473136) at v.dispatchResponse (c:\Users\ASUS\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\typescript-language-features\dist\extension.js:1:467106) at v.dispatchMessage (c:\Users\ASUS\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\typescript-language-features\dist\extension.js:1:465950) at ChildProcess.<anonymous> (c:\Users\ASUS\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\typescript-language-features\dist\extension.js:1:465443) at ChildProcess.emit (node:events:514:28) at emit (node:internal/child_process:951:14) at process.processTicksAndRejections (node:internal/process/task_queues:83:21)
tsc version : 5.4.5

@leonsbuddydave
Copy link

Doesn't seem to work when refactoring to an existing file either. I tried creating a new blank file manually and then refactoring to that file but VSCode ignores it.

Version info:
image

@mjbvz mjbvz transferred this issue from microsoft/vscode Jun 17, 2024
@RyanCavanaugh RyanCavanaugh assigned navya9singh and unassigned mjbvz Jun 18, 2024
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 18, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.6.0 milestone Jun 18, 2024
@DanielRosenwasser DanielRosenwasser added the Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output label Jul 2, 2024
@lemomar
Copy link

lemomar commented Jul 6, 2024

I'm having this problem in a Next.js project as well, is it specific to Next?

@nolanleung
Copy link

nolanleung commented Jul 7, 2024

Seems like the culprit is something in ^5.5 breaks refactoring imports from sym-linked packages.

Example (Works in v5.4.5 but not v5.5.2)

Running Refactor: Move to new file on SomeComponent correctly moves it and the Button import to SomeComponent.tsx, and updates the import in SomeOtherComponent.tsx to import { SomeComponent } from './SomeComponent'.

// apps/web/components/SomeOtherComponent.tsx

import { Button } from "@repo/ui";

export function SomeComponent() {
  return (
    <div>
      Some Component
      <Button>Click Me</Button>
    </div>
  );
}


export function SomeOtherComponent() {
  return (
    <div>
      <SomeComponent />
      <div>Some other component</div>
      <CheckList value={[]} options={[]} />
    </div>
  );
}

@etiennejcharles
Copy link

etiennejcharles commented Jul 10, 2024

I'm having this problem in a Next.js project as well, is it specific to Next?

No - It's caused by the typescript version you're using as commented by @nolanleung ealier

In your terminal:

  • npm i typescript@5.4.5 -D
  • Then In your command pallette - TypeScript: Select TypeScript Version..
  • Choose 5.4.5

And try the command, you'll see that it works ✅

@corned-beefhash
Copy link

corned-beefhash commented Jul 20, 2024

I'm having this problem in a Next.js project as well, is it specific to Next?

No - It's caused by the typescript version you're using as commented by @nolanleung ealier

In your terminal:

  • npm i typescript@5.4.5 -D
  • Then In your command pallette - TypeScript: Select TypeScript Version..
  • Choose 5.4.5

And try the command, you'll see that it works ✅

this fixed my issue. for anyone else

Error: <semantic> TypeScript Server Error (5.5.2)
Debug Failure. False expression.
Error: Debug Failure. False expression.
    at getCompletionEntryCodeActionsAndSourceDisplay

i was getting this error when attempting to autoimport certain things. TS server logs showed this

Info 908 [17:52:51.447] response:
{"seq":0,"type":"response","command":"getCodeFixes","request_seq":64,"success":true,"body":[{"fixName":"import","description":"Add import from \"@tanstack/react-table\"","changes":[{"fileName":"[redacted]/column-select-button.tsx","textChanges":[{"start":{"line":3,"offset":1},"end":{"line":3,"offset":1},"newText":"import type { Table } from \"@tanstack/react-table\";\n"}]}]}]}

but it wouldn't actually add any text to my file, even though it showed the correct autoimport in the list.

which means the error was not from TS server? But instead from vscode? Just posting this incase it helps anyone

@divmgl
Copy link

divmgl commented Jul 22, 2024

Also really annoying that the Move to file and Move to new file has been swapped. I've been using Move to new file for years in the first position, why swap it?

@anback
Copy link

anback commented Aug 3, 2024

This really screwed up my flow for about to days, downgrading helped. see #58897 (comment)

@Ife-Ody
Copy link

Ife-Ody commented Aug 8, 2024

  • TypeScript: Select TypeScript Version

This worked for me! Typescript version 5.4.5

@vitonsky
Copy link

In your terminal:

* `npm i typescript@5.4.5 -D`

* Then In your command pallette - `TypeScript: Select TypeScript Version..`

* Choose `5.4.5`

And try the command, you'll see that it works ✅

It works for me. If someone just faced with this problem - use this workaround

@sidouglas
Copy link

sidouglas commented Aug 14, 2024

Does not work for me unfortunately.
I added

"typescript.tsdk": "/Users/simon/.nvm/versions/node/v21.5.0/lib/node_modules/typescript/lib",

as per the docs

output error when moving a Type declaration to a type.ts file.

<semantic> TypeScript Server Error (5.4.5) Debug Failure. False expression: No interactive refactor arguments available Error: Debug Failure. False expression: No interactive refactor arguments available at Object.getRefactorEditsToMoveToFile [as getEditsForAction]
(/Users/simon/.nvm/versions/node/v21.5.0/lib/node_modules/typescript/lib/tsserver.js:140862:11) at Object.getEditsForRefactor
(/Users/simon/.nvm/versions/node/v21.5.0/lib/node_modules/typescript/lib/tsserver.js:139733:31) at getEditsForRefactor2 (/Users/simon/.nvm/versions/node/v21.5.0/lib/node_modules/typescript/lib/tsserver.js:147297:32) at /Users/simon/.vscode/extensions/astro-build.astro-vscode-2.13.3-darwin-arm64/node_modules/astro-ts-plugin-bundle/index.js:5:1884 at Proxy.
<anonymous> (/Users/simon/.vscode/extensions/vue.volar-2.0.28/node_modules/typescript-vue-plugin-bundle/index.js:5:1894) at IpcIOSession.getEditsForRefactor (/Users/simon/.nvm/versions/node/v21.5.0/lib/node_modules/typescript/l...

@KostkaBrukowa
Copy link

@sidouglas i guess in your case @vue/typescript-plugin plugin is the issue. I've had the same problem, and when I removed this plugin the refactor worked

@rsxdalv
Copy link

rsxdalv commented Sep 10, 2024

The workaround unfortunately doesn't work as smoothly if you don't have a node_modules in the current workspace root, i.e., if you have a monorepo with another sub folder.

I also tried the nightly version (via extension typescript nightly) 5.7 and it's broken too. If I do move to file I at the very minimum least see an error. But trying to move to a new file anything bigger than a constant number fails.

@goncalossantos
Copy link

This was also happening to me due to the default VSCode version of TS, even though I was working without typescript on a js file. Doing the steps above and then changing the tsdk version fixed it for me

@iisaduan
Copy link
Member

Minimal repro:

/// <reference path='fourslash.ts' />

// @Filename: /node_modules/zod/types.d.ts
//// declare const objectType: () => {};
//// 
//// export { objectType as object };

// @Filename: /node_modules/zod/external.d.ts
//// export * from "./types";

// @Filename: /node_modules/zod/index.d.ts
//// import * as z from "./external";
//// export * from "./external";
//// export { z };
//// export default z;

// @Filename: /test.ts
//// import { z } from "zod";
//// 
//// [|const createIssueSchema = z.object();|]

verify.moveToNewFile({
    newFileContents: {
        "/test.ts":
`
`,

        "/createIssueSchema.ts":
`import { z } from "zod";


const createIssueSchema = z.object();
`,
    },
});

MoveToNewFile should stop crashing after #60333, but the minimal repro will generate that z is imported from zod/external instead of just z

@laugri
Copy link

laugri commented Nov 12, 2024

  • Then In your command pallette - TypeScript: Select TypeScript Version..

For those like me that don't understand why they don't have the entry showing up in their command palette, your focused tab has to be a .ts(x) file.

@magicdawn
Copy link

  • Then In your command pallette - TypeScript: Select TypeScript Version..

For those like me that don't understand why they don't have the entry showing up in their command palette, your focused tab has to be a .ts(x) file.

This is another problem, I use "restart ts server" a lot, since problems with ts server can not auto recover, I have to manual restart.
I have to open a ts/tsx file then use the command palette to restart.
why not showing these entries for whole workspace if ts server is running ? @the Team ???

@magicdawn
Copy link

"move to file" / "move to new file" still not working on TypeScript v5.5 v5.6 & latest v5.7.2

Image


"move" works on v5.4, but TS v5.5 Inferred Type Predicates is really awesome, Can't leave this.

@iisaduan
Copy link
Member

@magicdawn since your crash still happens on 5.7, will you open a new issue with your repro? The case originally reported in this thread should no longer crash in 5.7, and I'd need to investigate your case separately

@iisaduan iisaduan changed the title refactor: New file doesn't work refactor: New file generates import from wrong module specifier Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output
Projects
None yet
Development

No branches or pull requests