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

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 this issue Nov 25, 2024 · 2 comments

Comments

@martinhj
Copy link

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
  2. Go to a defined groq query in the project
  3. Click on Execute Query above the query
  4. 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

image

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 #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);
@lcnogueira
Copy link

I've experienced the same problem and I was expecting the plugin to read the data from the sanity.cli.ts file , regardless of the fact that the data is coming from a .env file.

@sahandii
Copy link

Same here, can't make it work with .env variables. it's a shame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants