Skip to content

Commit dd413d8

Browse files
Luca ForstnerLms24
andauthored
fix(core): Stop .env files from being picked up (#486)
Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
1 parent ca1533b commit dd413d8

File tree

1 file changed

+13
-10
lines changed
  • packages/bundler-plugin-core/src

1 file changed

+13
-10
lines changed

packages/bundler-plugin-core/src/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,20 @@ export function sentryUnpluginFactory({
7878
debug: userOptions.debug ?? false,
7979
});
8080

81-
const dotenvResult = dotenv.config({
82-
path: path.join(process.cwd(), ".env.sentry-build-plugin"),
83-
});
84-
85-
// Ignore "file not found" errors but throw all others
86-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
87-
// @ts-ignore Accessing `code` on error should be safe
88-
if (dotenvResult.error && dotenvResult.error.code !== "ENOENT") {
89-
throw dotenvResult.error;
90-
} else if (dotenvResult.parsed) {
81+
try {
82+
const dotenvFile = fs.readFileSync(
83+
path.join(process.cwd(), ".env.sentry-build-plugin"),
84+
"utf-8"
85+
);
86+
// NOTE: Do not use the dotenv.config API directly to read the dotenv file! For some ungodly reason, it falls back to reading `${process.cwd()}/.env` which is absolutely not what we want.
87+
const dotenvResult = dotenv.parse(dotenvFile);
88+
process.env = { ...process.env, ...dotenvResult };
9189
logger.info('Using environment variables configured in ".env.sentry-build-plugin".');
90+
} catch (e: unknown) {
91+
// Ignore "file not found" errors but throw all others
92+
if (typeof e === "object" && e && "code" in e && e.code !== "ENOENT") {
93+
throw e;
94+
}
9295
}
9396

9497
const options = normalizeUserOptions(userOptions);

0 commit comments

Comments
 (0)