sanity-io / vscode-sanity

Visual Studio Code extension for developing applications powered by Sanity.io
MIT License
51 stars 9 forks source link

Sanity.io VSCode Developer tools is not able to read projectId and dataset from `sanity.cli.ts` when it reads from environment variables #29

Open martinhj opened 6 days ago

martinhj commented 6 days ago

Describe the bug

Running a query with 'Execture Query' in the Sanity.io VSCode extension results in a "Configuration must contain projectId" when sanity.cli.ts config file is set up to get projectId and dataset from env variables defined in a .env.local -file in the project root.

The sanity cli tool works (pnpm exec sanity dataset list)

To Reproduce

Steps to reproduce the behavior:

  1. Create the file sanity.cli.ts with the following
    
    import { defineCliConfig } from "sanity/cli";

export const projectId = process.env.SANITY_PROJECT_ID; export const dataset = process.env.SANITY_DATASET;

export default defineCliConfig({ api: { projectId, dataset } });

1. Add SANITY_PROJECT_ID and SANITY_DATASET to .env.local
1. Go to a defined groq query in the project
1. Click on Execute Query above the query
1. See error "Configuration must contain `projectId`" in the VSCode Notification

**Expected behavior**

The expected behaviour is that the VSCode Sanity.io plugin is able to parse and use the sanity.cli.ts config file the same way the sanity cli tool does.

The same expectation after reading from https://github.com/sanity-io/vscode-sanity?tab=readme-ov-file#execute-groq-queries which does not state that defining this statically is required:
> The project ID and dataset used is determined by finding sanity.cli.ts in the workspace. If multiple files are found, the extension will prompt you to select one.

Renaming .env.local to .env was also attempted, but did not resolve the issue or change the outcome.

If, in sanity.cli.ts, the projectId and dataset is replaced with static strings Sanity.io in VSCode works, this happens only if the values are attempted read from the env variables / env-var config file.

**Screenshots**

<img width="316" alt="image" src="https://github.com/user-attachments/assets/f1afee45-d9c7-4904-986e-fa79a27f83fc">

**Which versions of Sanity are you using?**

VSCode extension _Developer tools for applications powered by Sanity.io_ v0.2.1

pnpm exec sanity versions @sanity/cli (global) 3.64.3 (up to date) @sanity/asset-utils 2.2.0 (up to date) @sanity/dashboard 4.1.0 (up to date) @sanity/icons 3.4.0 (up to date) @sanity/image-url 1.1.0 (up to date) @sanity/preview-url-secret 1.6.21 (latest: 2.0.4) @sanity/react-loader 1.10.20 (up to date) @sanity/vision 3.64.3 (up to date) sanity 3.64.3 (up to date)


**What operating system are you using?**
MacOs 15.1

**Which versions of Node.js / npm are you running?**
nodejs: v20.17.0
npm:    v10.8.2

**Additional context**

This issue seams related and increase the expectation for the ability to read env variables from file https://github.com/sanity-io/vscode-sanity/issues/17

This is within a nextjs project, but since the sanity cli tool works with the .env.local -file I expect this to work with the Sanity VSCode extension as well.

Also attempted to explicitly read the env-conf with NextJs tooling but resulted in the same issue. These line was added to the sanity.cli.ts file:

import { loadEnvConfig } from "@next/env"; const projectDir = process.cwd(); loadEnvConfig(projectDir);