forcedotcom / cli

Salesforce CLI
https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/
BSD 3-Clause "New" or "Revised" License
474 stars 77 forks source link

`Error (1): Unexpected token '', "{ "orgN"... is not valid JSON` when trying to create a scratch org with a namespace #2913

Closed SCWells72 closed 3 weeks ago

SCWells72 commented 3 weeks ago

Summary

When trying to create a scratch org with a namespace, I get an error:

$ sf org create scratch -v <devHubAlias> -a <alias> -y 30 -f config/namespace-scratch-def.json -w 60
Error (1): Unexpected token '', "{
  "orgN"... is not valid JSON

Here's the sfdx-project.json:

{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true
    }
  ],
  "name": "<name>",
  "namespace": "<namespace>",
  "sfdcLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "60.0"
}

Note that <namespace> is a namespace that has already been registered with <devHubAlias>, and I've successfully created scratch orgs with that namespace in the past. This seems to be a recent regression.

System Information

Happens in both cmd.exe and bash. Here's the version info:

$ sf version --verbose --json
{
  "architecture": "win32-x64",
  "cliVersion": "@salesforce/cli/2.44.8",
  "nodeVersion": "node-v20.13.1",
  "osVersion": "Windows_NT 10.0.22631",
  "rootPath": "C:\\Users\\Scott\\AppData\\Local\\sf\\client\\2.44.8-c60512e",
  "shell": "cmd.exe",
  "pluginVersions": [
    "@oclif/plugin-autocomplete 3.0.18 (core)",
    "@oclif/plugin-commands 3.3.4 (core)",
    "@oclif/plugin-help 6.0.22 (core)",
    "@oclif/plugin-not-found 3.1.10 (core)",
    "@oclif/plugin-plugins 5.1.3 (core)",
    "@oclif/plugin-search 1.0.24 (core)",
    "@oclif/plugin-update 4.2.14 (core)",
    "@oclif/plugin-version 2.1.2 (core)",
    "@oclif/plugin-warn-if-update-available 3.0.19 (core)",
    "@oclif/plugin-which 3.1.10 (core)",
    "@salesforce/cli 2.44.8 (core)",
    "apex 3.1.13 (core)",
    "auth 3.6.17 (core)",
    "data 3.4.3 (core)",
    "deploy-retrieve 3.8.3 (core)",
    "info 3.3.3 (core)",
    "limits 3.3.9 (core)",
    "marketplace 1.2.8 (core)",
    "org 4.1.14 (core)",
    "packaging 2.4.5 (core)",
    "schema 3.3.9 (core)",
    "settings 2.2.7 (core)",
    "sobject 1.4.6 (core)",
    "source 3.3.18 (core)",
    "telemetry 3.3.11 (core)",
    "templates 56.2.9 (core)",
    "trust 3.7.3 (core)",
    "user 3.5.11 (core)",
    "@salesforce/sfdx-scanner 4.2.0 (user) published 14 days ago (Tue May 28 2024) (latest is 3.25.0)"
  ]
}
github-actions[bot] commented 3 weeks ago

Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.

cristiand391 commented 3 weeks ago

"orgN"... is not valid JSON

do you have orgName set in config/namespace-scratch-def.json? can you check if it's valid JSON (jq config/namespace-scratch-def.json)?

I tried on sf v2.46.5 (current nightly) and can't repro using this scratch definition file:

{
    "orgName": "Dreamhouse",
    "edition": "Developer",
    "namespace": "cristiand391",
    "features": ["Walkthroughs", "EnableSetPasswordInApi"],
    "settings": {
        "lightningExperienceSettings": {
            "enableS1DesktopEnabled": true
        },
        "mobileSettings": {
            "enableS1EncryptedStoragePref2": false
        }
    }
}
SCWells72 commented 3 weeks ago

@cristiand391, here are the exact contents of config/namespace-scratch-def.json:

{
  "orgName": "external_services_ns",
  "edition": "Developer",
  "hasSampleData": false,
  "features": [
    "EnableSetPasswordInApi"
  ],
  "settings": {
    "lightningExperienceSettings": {
      "enableS1DesktopEnabled": true
    },
    "mobileSettings": {
      "enableS1EncryptedStoragePref2": false
    }
  }
}

Note that I also tried adding "namespace": "<namespace>" to that file and it fails in the same manner.

Let me know if I can provide anything else.

cristiand391 commented 3 weeks ago

@SCWells72 I tried installing the same node and sf versions but I can't repro on macos, I'll give it a try on windows tomorrow and report back.

FWIW, I don't see any recent change in JSON.parse in node, the error you get says you have a wrong JSON (from your examples it seems it's config/namespace-scratch-def.json). If you can, passing --dev-debug should make it print the stacktrace where the JSON parsing is failing.

SCWells72 commented 3 weeks ago

@cristiand391, here you go:

*** Internal Diagnostic ***

SyntaxError: Unexpected token '', "{
  "orgN"... is not valid JSON
    at JSON.parse (<anonymous>)
    at buildScratchOrgRequest (file:///C:/Users/Scott/AppData/Local/sf/client/2.44.8-c60512e/node_modules/@salesforce/plugin-org/lib/shared/scratchOrgRequest.js:16:20)
    at async EnvCreateScratch.run (file:///C:/Users/Scott/AppData/Local/sf/client/2.44.8-c60512e/node_modules/@salesforce/plugin-org/lib/commands/org/create/scratch.js:152:38)
    at async EnvCreateScratch._run (C:\Users\Scott\AppData\Local\sf\client\2.44.8-c60512e\node_modules\@oclif\core\lib\command.js:311:22)
    at async Config.runCommand (C:\Users\Scott\AppData\Local\sf\client\2.44.8-c60512e\node_modules\@oclif\core\lib\config\config.js:433:25)
    at async run (C:\Users\Scott\AppData\Local\sf\client\2.44.8-c60512e\node_modules\@oclif\core\lib\main.js:92:16)
******
cristiand391 commented 3 weeks ago

it's reading your config/namespace-scratch-def.json here:

https://github.com/salesforcecli/plugin-org/blob/3a1818f3f80868566a37fcc79d26a10f7fdf28e4/src/shared/scratchOrgRequest.ts#L23

that says your file isn't valid JSON.

If you can validate your JSON and still repro then the only thing I can think of is some of the changes done to JSON.parse in node v20 (can't find them in node's changelog). It broke some of our tests a while ago but it was updates to the error handler: https://github.com/forcedotcom/kit/pull/236

I can't repro on windows.

SCWells72 commented 3 weeks ago

@cristiand391, here's the exact file that's resulting in the error:

namespace-scratch-def.json

It's showing up as valid in IntelliJ IDEA, VS Code, etc. Are you able to use it to create a scratch org? Well, I assume it'll fail because you don't have access to that namespace -- and editing the file will obviously risk changing it -- but hopefully you'll know quickly whether or not it gets past the specific error that I'm seeing.

shetzel commented 3 weeks ago

That file begins with a BOM (U+FEFF char) and the code reads the file as UTF-8 so it's interpreting it as a non-JSON parseable character. I would try to regenerate that file or look into how it's being generated.

SCWells72 commented 3 weeks ago

Wow...that's bizarre. That file was literally copied directly from the existing project-scratch-def.json file which works just fine. I wonder what added that BOM to its start. I also wonder why neither dev tool actually designated that as a potential problem, though to be fair, IntelliJ IDEA did show that as a characteristic of the file encoding when I knew to check there:

image

Anyway, obviously it works fine now. Strange that nothing actually identified that as a potential problem, though...

Thanks for taking the time to help troubleshoot, and glad to hear it was user error and not a real problem!

shetzel commented 3 weeks ago

I'm glad we got this figured out! Thanks for sharing the file.