Shelvacu
Shelvacu

Reputation: 4362

VSCode Extension: How to debug "Activating extension 'undefined_publisher.extension-name' failed: Unexpected token {."

I have created a vscode extension using typescript. Normally, I'd reduce the problem to the smallest possible piece of code, but whenever I try to do that, the problem disappears.

Whenever I try to run the extension (by pressing Ctrl+Shift+P and selecting my extension's command), I get the error: (name changed)

Activating extension 'undefined_publisher.extension-name' failed: Unexpected token {.

Or, in previous versions of the code when I first encountered the error, ...Unexpected token ,.

The error gives no indication of which line or even which file contains the unexpected token. I have tried to run the debugger on the code that loads the extension (vscode devtools), but I couldn't find any obvious "current file" or some such in the call stack.

How do I even begin to debug this?

package.json: (name changed)

{
    "name": "extension-name",
    "displayName": "Extension Name",
    "description": "Extension Descripton",
    "version": "0.0.1",
    "engines": {
        "vscode": "^1.41.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": [
        "onCommand:extension-name.start"
    ],
    "main": "./out/extension.js",
    "contributes": {
        "commands": [
            {
                "command": "extension-name.start",
                "title": "Open a Extension Name window"
            }
        ]
    },
    "scripts": {
        "vscode:prepublish": "npm run compile",
        "compile": "tsc -p ./",
        "watch": "tsc -watch -p ./",
        "pretest": "npm run compile",
        "test": "node ./out/test/runTest.js"
    },
    "devDependencies": {
        "@types/glob": "^7.1.1",
        "@types/mocha": "^5.2.7",
        "@types/node": "^12.11.7",
        "@types/vscode": "^1.41.0",
        "awesome-typescript-loader": "^5.2.1",
        "glob": "^7.1.5",
        "html-loader": "^0.5.5",
        "mocha": "^6.2.2",
        "tslint": "^5.20.0",
        "typescript": "^3.6.4",
        "vscode-test": "^1.2.2",
        "webpack": "^4.41.2",
        "webpack-cli": "^3.3.9"
    },
    "dependencies": {
        "cerialize": "^0.1.18",
        "ts_common": "file:../ts_common"
    },
    "private": true
}

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2016",
        "outDir": "out",
        "lib": [
            "es2018",
            "dom"
        ],
        "sourceMap": true,
        //"rootDir": "src",
        "typeRoots": ["node_modules/@types"],    
        "strict": true,   /* enable all strict type-checking options */
        /* Additional Checks */
        "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
        "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
        // "noUnusedParameters": true,  /* Report errors on unused parameters. */
    },
    "awesomeTypescriptLoaderOptions": {
      "useCache": true,
      "isolatedModules": true,
      //"transpileOnly": true, // This is about a 2x speedup to compilation but disable type errors/warnings
      "reportFiles": [
        "src/**/*.{ts,tsx}"
      ]
    },  
    "include": [
      "./src/"
    ]/*,
    "exclude": [
        "node_modules",
        "../ts_common",
        "./dist",
        ".vscode-test"
    ]*/
}

Upvotes: 5

Views: 6611

Answers (4)

Zach Wolf
Zach Wolf

Reputation: 21

I ran into this because when following the your first extension guide I had selected Bundle the source code with webpack? yes, so the code needs to be compiled.

Run npm run watch or npm run build and then normal plugin run.

Upvotes: 2

Shelvacu
Shelvacu

Reputation: 4362

I was able to solve my issue by changing the target option in tsconfig.json from es2016 to es5. I did not try any others; es6/es2015 may also work.

I found this by changing around my code basically randomly until I got an error like unexpected token "export", at which point I suspected the target version.

Upvotes: 3

Jack Lu
Jack Lu

Reputation: 170

Same with you. I tried surrounding the problematic statement with try, but nothing was caught.

After some examination I found it's caused by optional chaining syntax in my source code, then I realized my project is initialized before ts 3.7 arrived so it's not supported. Checking the compiler output in Terminal window confirmed this. (I didn't notice it because current vscode comes with ts 3.7 so no error is highlighted in source code.) The error is not in program execution. It's syntax error during compilation. Check it.

Upvotes: 3

Dan
Dan

Reputation: 101

Are you using "export" in an embedded script?

From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export You can't do that.

Upvotes: 0

Related Questions