microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.16k stars 12.38k forks source link

Refactor: move to new file does not work #59650

Open a-x- opened 4 weeks ago

a-x- commented 4 weeks ago

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

Steps to Reproduce:

  1. Refactor > move to new file

What I tried already:

  1. Run with --disable-extensions to ensure it's not a problem with a package.
  2. typescript.tsserver.enableTrace
  3. find minimal examples
  4. search for similar issues: https://github.com/microsoft/vscode/issues/51586, https://github.com/microsoft/vscode/issues/58711

It worked a few versions before. I don't know exactly what version was okay.

UPD: not working:

import { useLatest } from 'react-use';

export function useUseLatest (data: string) {
    return useLatest(data);
}

not working:

import { selectStore, useSelector } from '@proterm/core';

export function useOrders() {
    return useSelector(selectStore);
}

not working:

import { selectStore } from '@proterm/core';

export function useOrders() {
    return selectStore;
}

working:

import { AnyBaseOrder } from '@terminal/scalp/types';

export function useOrders(symbolId: string): AnyBaseOrder[] {}

working:

import { useSelector } from 'react-redux';

function useOrders() {
    return useSelector((state) => console.log(state));
}

working:

import { selectStore, useSelector } from '@proterm/core';
export function useOrders(symbolId: string): AnyBaseOrder[] {}

I though @ import aliases is a problem, however this works too:

import { FancyAlertCircleIcon } from '@terminal/icons';

export function useOrders() {
    return FancyAlertCircleIcon;
}

error:

2024-08-15 18:42:15.101 [error] <semantic> TypeScript Server Error (5.5.4)
Debug Failure.
Error: Debug Failure.
    at Object.addImportFromExportedSymbol (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:154243:32)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144288:19
    at Map.forEach (<anonymous>)
    at addTargetFileImports (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144282:17)
    at getNewStatementsAndRemoveFromOldFile (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:143512:3)
    at doChange4 (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144488:3)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144477:77
    at _ChangeTracker.with (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:174306:5)
    at Object.getRefactorEditsToMoveToNewFile [as getEditsForAction] (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144477:60)
    at Object.getEditsForRefactor (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:142603:31)
    at Object.getEditsForRefactor2 [as getEditsForRefactor] (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:149939:32)
    at IpcIOSession.getEditsForRefactor (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191087:49)
    at getEditsForRefactor (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:189305:43)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191491:69
    at IpcIOSession.executeWithRequestId (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191483:14)
    at IpcIOSession.executeCommand (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191491:29)
    at IpcIOSession.onMessage (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191533:51)
    at process.<anonymous> (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/tsserver.js:523:14)
    at process.emit (node:events:519: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.4)
a-x- commented 4 weeks ago

tsconfig.json:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "resolveJsonModule": true,
        "outDir": "./dist/",
        "lib": ["es2018", "dom", "ES2020.Promise", "DOM.Iterable", "es2019.array", "ES2021.String"],
        "sourceMap": true,
        "strict": true,
        "module": "esnext",
        "jsx": "react-jsx",
        "target": "es2017",
        "rootDir": ".",
        "allowJs": true,
        "moduleResolution": "node",
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "downlevelIteration": true,
        "importHelpers": true,
        "experimentalDecorators": true,
        "preserveSymlinks": true,
        "useUnknownInCatchVariables": false,

        // COPY: /webpack/tsconfig.json
        // COPY: /webpack/webpack.base.config.ts#alias
        // COPY: /.storybook/main.ts
        "paths": {
            "@proterm/core": ["./src/core"],
            "@terminal/*": ["./src/*"]
        },
        "plugins": [
            {
                "name": "typescript-plugin-css-modules",
                "options": {
                    "customMatcher": "\\.less$"
                }
            }
        ]
    },
    "types": ["node", "jest", "@testing-library/jest-dom", "@testing-library/react"],
    "include": ["./src/"],
    "exclude": ["**/__tests__", "**/*.stories.*", "**/__mock__/*.ts", "**/node_modules", "**/lib"]
}
a-x- commented 4 weeks ago

So, @proterm/core is an alias to the big file with re-exports and re-exports of re-exports

Moreover, selectStore is declared as weird as:

function selectStore<State>(store: ReducerStore<State>): (state: any) => State;
function selectStore<State>(store: ReducerStore<State>, state: any): State;
function selectStore<State>(store: ReducerStore<State>, state?: any) {
    if (state) {
        return select(store, state);
    }

    function selectStoreCurried(stateCurried: State) {
        return select(store, stateCurried);
    }

    (selectStoreCurried as any)[widgetScope.symbol] = true;

    return selectStoreCurried;
}

(selectStore as any)[widgetScope.symbol] = true;

export default selectStore;
a-x- commented 4 weeks ago

Looks like this is a problem, because

import { syncSettings } from '@proterm/core';

export function useOrders() {
    return syncSettings;
}

works as syncSettings hasn't multiple declarations.

In the save moment, I cannot move other functions w/o referring selectStore or things like this. For instance this does not work too:

import { useCallback, useRef, useState } from 'react';
import { useLatest } from 'react-use';

type Opts = { lastOnly?: boolean };
type Set<D> = (data: D) => void;
export function useSubscriptionProvider<T extends Set<D>, D>(name: string, data: D, opts?: Opts) {
    const { lastOnly = true } = opts || {};
    const [buffer, setBuffer] = useState<D[]>([]);
    const queueRef = useLatest(buffer);
    const saveBuffer = useCallback((data: D) => setBuffer((q) => [...q, data]), []);
    const ref = useRef<T>();
    console.log('hm', { lastOnly, buffer, queueRef, saveBuffer, ref, name, data });
}

Currently problem is a useLatest from 'react-use',

Minimal not working example:

import { useLatest } from 'react-use';

export function useUseLatest (data: string) {
    return useLatest(data);
}
Jason3S commented 3 weeks ago

I'm seeing this issue too.

When I try to Move to a New File in vscode, the following errors appear in the Extension Host output.

2024-08-16 14:51:23.411 [error] [vscode.typescript-language-features] provider FAILED
2024-08-16 14:51:23.411 [error] Error: <semantic> TypeScript Server Error (5.5.4)
Debug Failure. False expression: Changes overlap
Verbose Debug Information: {"pos":0,"end":1125} and {"pos":455,"end":455}
Error: Debug Failure. False expression: Changes overlap
Verbose Debug Information: {"pos":0,"end":1125} and {"pos":455,"end":455}
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:174932:15
aureliopetrone commented 2 weeks ago

@navya9singh any update on this?

robMolloy commented 1 week ago

@navya9singh @aureliopetrone @sheetalkamat this looks like the fix for a similar issue however it's not working for me https://github.com/microsoft/TypeScript/pull/59625

I have just downloaded the newest version and it doesn't seem to work for me.

Additionally there is some comments about disabling/enabling "typescript.tsserver.experimental.useVsCodeWatcher". I have tried both enabled and disabled but neither resolves for me https://github.com/microsoft/TypeScript/issues/59603#issuecomment-2285035053

sheetalkamat commented 1 week ago

This seems more or less duplicate of #58740

komichar commented 9 hours ago

I can confirm that this bug is still present & refactoring is broken for several minor & fix versions. Any plans to fix this?