Repository navigation
Replies: 3 comments
|
Thanks for the report. I reproduced this against commit 47f9438. The loader has two independent .env reads, and both currently attempt to read the path without first checking that it is a regular file, so a directory named .env surfaces EISDIR.\n\nI prepared a minimal local patch (commit b15c8a3): missing paths and non-regular paths are silently skipped, while read failures for regular files keep the existing warning behavior. Layered precedence and bootstrap-only validation remain unchanged. Focused tests pass (52/52), along with package TypeScript, Oxlint, and doc-sync (28/28).\n\nSince external PRs are currently not accepted, could a maintainer confirm whether this behavior and patch direction fit the project expectations? I can provide the patch or adapt it to the project conventions. |
|
这个复现已经把两层 .env reader 和 EISDIR/Node 24 的差异说明得很清楚。补充一个实现上的安全边界:可以忽略明确的“存在但不是普通 dotenv 文件”的情况,但不能把所有非 ENOENT 错误都当成缺失。 建议修复时考虑:
独立社区 handbook 的凭证与启动边界参考: https://github.com/sandbaseai/deepseek-harness-handbook/blob/main/docs/en/troubleshooting/auth-token-not-available.md 本 handbook 是独立社区项目,并非 DeepSeek AI 官方项目。 |
|
I reproduced this against the current Root cause. Both Fix. Check with Tests. Four tests cover the changed behaviour: a directory Patch --- a/packages/boot/app-boot/src/index.ts
+++ b/packages/boot/app-boot/src/index.ts
@@ -10,7 +10,7 @@
-import { readFileSync } from 'node:fs'
+import { readFileSync, statSync } from 'node:fs'
@@ -110,17 +110,29 @@
+/**
+ * Whether `path` exists and is a regular file. Returns `false` for missing
+ * paths, directories, FIFOs, sockets, and devices — none of which can hold
+ * dotenv content.
+ */
+function isRegularFile(path: string): boolean {
+ try {
+ return statSync(path).isFile()
+ } catch {
+ return false
+ }
+}
+
export function loadEnv(
binName: string, dir: string = process.cwd(),
warn: (line: string) => void = line => void process.stderr.write(line),
): void {
+ const path = resolve(dir, '.env')
+ if (!isRegularFile(path)) return
try {
- process.loadEnvFile(resolve(dir, '.env'))
+ process.loadEnvFile(path)
} catch (error) {
- if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') {
- warn(`${binName}: failed to load .env: ${String(error)}\n`)
- }
- // ENOENT (no .env) is fine — rely on the ambient environment.
+ warn(`${binName}: failed to load .env: ${String(error)}\n`)
}
}
@@ -189,14 +201,12 @@
function readEnvLayer(
binName: string, dir: string, warn: (line: string) => void, home: string,
): { path: string; values: Record<string, string> } | undefined {
const path = resolve(dir, '.env')
const isHome = resolve(dir) === home
+ if (!isRegularFile(path)) return undefined
let content: string
try {
content = readFileSync(path, 'utf8')
} catch (error) {
- if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') {
- warn(`${binName}: failed to load .env: ${String(error)}\n`)
- }
- // ENOENT (no .env) is fine — rely on the ambient environment.
+ warn(`${binName}: failed to load .env: ${String(error)}\n`)
return undefined
}The full patch, including tests and both READMEs, is attached below — I understand external PRs are not being accepted right now, so I am posting it here rather than opening a PR. Happy to adjust the shape or split it if that helps. This aligns with the direction @ltyfulan9 sketched (silently skip non-regular paths) and the classification concerns @liyangbing raised (the Full patch (git am)From 21c44abe5a0a24d26ff0f4f27d6293d1023f9b61 Mon Sep 17 00:00:00 2001
From: lilei0311 <lilei0311@users.noreply.github.com>
Date: Sun, 27 Sep 2026 00:36:47 +0800
Subject: [PATCH] fix(app-boot): skip a directory .env silently instead of
warning EISDIR
Both .env readers in app-boot treated only ENOENT as "no file", so a
directory named `.env` in the invoking directory or Harness home surfaced
`EISDIR` (or `ERR_INVALID_ARG_TYPE` from process.loadEnvFile) as a labelled
warning on every boot. A directory can never hold dotenv content, so per the
documented semantics it should fall back to the ambient environment like a
missing file.
Check with statSync whether the resolved path is a regular file before
reading; missing paths and non-regular files (directories, FIFOs, sockets,
devices) are skipped silently, while read failures on regular files keep the
existing warning. Both loadEnv and readEnvLayer share the same classifier.
Update both READMEs to distinguish the three cases and re-record the pairing
entry.
---
packages/boot/app-boot/README.i18n.yaml | 4 +-
packages/boot/app-boot/README.md | 2 +-
packages/boot/app-boot/README.zh.md | 2 +-
packages/boot/app-boot/src/index.ts | 42 +++++++----
packages/boot/app-boot/tests/app-boot.spec.ts | 69 ++++++++++++++++---
5 files changed, 92 insertions(+), 27 deletions(-)
diff --git a/packages/boot/app-boot/README.i18n.yaml b/packages/boot/app-boot/README.i18n.yaml
index bd29896a32..4a94dc0b04 100644
--- a/packages/boot/app-boot/README.i18n.yaml
+++ b/packages/boot/app-boot/README.i18n.yaml
@@ -24,8 +24,8 @@
en: 1099528bde0e2aee
zh: b68772bdb9bb7856
/deepseek-ai-dsh-app-boot/use-this-package/profiles:
- en: d50b0fdaf62353dd
- zh: 0fccb6968602e5de
+ en: d78d29b580c7370c
+ zh: 5286434b4c2652b1
/deepseek-ai-dsh-app-boot/use-this-package/previewing-the-effective-configuration:
en: 646a0ccdd7881ad1
zh: e3696acb1573dd7b
diff --git a/packages/boot/app-boot/README.md b/packages/boot/app-boot/README.md
index e9bae8f9be..3032b09795 100644
--- a/packages/boot/app-boot/README.md
+++ b/packages/boot/app-boot/README.md
@@ -57,7 +57,7 @@ Exact-version exemptions live in the profile's own `compatibility.json`, not in
Your machine-local preferences also live in the Harness home:
-- **`.env`** — your ordinary environment layers: the invoking directory's file outranks the Harness-home file, and both sit below the inherited environment. Variables that decide how the process starts (`PATH`, `DSH_*`, `XDG_*` and similar) are rejected from files: export them instead. The four proxy names (`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, `NO_PROXY`) are accepted from the Harness-home file only, never from the invoking directory's, which arrives with a clone. For a non-product bin that just wants one directory's `.env`, a missing file is fine and an unloadable one prints one labelled warning line.
+- **`.env`** — your ordinary environment layers: the invoking directory's file outranks the Harness-home file, and both sit below the inherited environment. Variables that decide how the process starts (`PATH`, `DSH_*`, `XDG_*` and similar) are rejected from files: export them instead. The four proxy names (`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, `NO_PROXY`) are accepted from the Harness-home file only, never from the invoking directory's, which arrives with a clone. For a non-product bin that just wants one directory's `.env`, a missing path or a non-regular file (directory, FIFO, socket, device) is fine — both fall back to the ambient environment silently; only a regular file that cannot be read prints one labelled warning line.
- **`cordis.patch.yml`** — your tweak layer, applied after every bundle layer (per-profile first, then the home-level file, which therefore outranks it): replace one entry's whole config (restating the fields you keep), insert new entries, or interpolate `!!js` expressions at boot. A patch naming an entry that does not exist prints a stderr warning; an empty or comments-only file fails boot — disable the layer with `[]` instead.
The enabled `dsh-hmr` plugin watches the profile manifest and both user patch files, re-reads the ordered bundle layers, and applies the [reload failure policy](#startup-and-reload-failures). [DSH HMR](../hmr/README.md) serializes these reloads with [Plugin Manager](../plugin-manager/README.md) configuration writes; package operations run outside its queue. The launcher does not install HMR or watchers; disabled or absent HMR means changes require restart.
diff --git a/packages/boot/app-boot/README.zh.md b/packages/boot/app-boot/README.zh.md
index ff4034d31e..a96101aaf8 100644
--- a/packages/boot/app-boot/README.zh.md
+++ b/packages/boot/app-boot/README.zh.md
@@ -57,7 +57,7 @@ profile 导入插件前,DSH 会检查其 `peerDependencies` 中对 `@deepseek-
你的机器本地偏好同样位于 harness home 中:
-- **`.env`**——你的普通环境层:调用目录的文件优先于 harness home 的文件,两者都低于继承环境。在文件中设置的进程启动变量(如 `PATH`、`DSH_*`、`XDG_*`)会被拒绝:请改为导出这些变量。四个代理名(`HTTP_PROXY`、`HTTPS_PROXY`、`ALL_PROXY`、`NO_PROXY`)只从 harness home 的文件接受,绝不从调用目录的文件接受——后者随 clone 一起到来。对于只想加载某个目录 `.env` 的非产品 bin,文件缺失不影响启动,文件无法加载时输出一行带标签的警告。
+- **`.env`**——你的普通环境层:调用目录的文件优先于 harness home 的文件,两者都低于继承环境。在文件中设置的进程启动变量(如 `PATH`、`DSH_*`、`XDG_*`)会被拒绝:请改为导出这些变量。四个代理名(`HTTP_PROXY`、`HTTPS_PROXY`、`ALL_PROXY`、`NO_PROXY`)只从 harness home 的文件接受,绝不从调用目录的文件接受——后者随 clone 一起到来。对于只想加载某个目录 `.env` 的非产品 bin,路径缺失或非普通文件(目录、FIFO、socket、设备)都不影响启动——两者都静默回退到环境变量;只有无法读取的普通文件才输出一行带标签的警告。
- **`cordis.patch.yml`**——你的 tweak 层,应用在所有组合包层之后(先应用逐 profile 的文件,再应用 home 级文件,因此后者优先级更高):替换某个条目的整个配置(重述你要保留的字段)、插入新条目,或在启动时插值 `!!js` 表达式。patch 指定的条目不存在时输出 stderr 警告;空文件或仅含注释的文件会导致启动失败——如需禁用该层,请改用 `[]`。
启用的 `dsh-hmr` 插件会监视 profile manifest 与两份用户 patch 文件,重新读取按顺序排列的组合包层,并应用[重载失败策略](#startup-and-reload-failures)。[DSH HMR](../hmr/README.zh.md) 将这些重载与[插件管理器](../plugin-manager/README.zh.md)的配置写入串行化;包操作在其队列之外执行。启动器不安装 HMR 或监视器;HMR 被禁用或不存在时,更改需要重启。
diff --git a/packages/boot/app-boot/src/index.ts b/packages/boot/app-boot/src/index.ts
index 188cb33460..28ef76a0b5 100644
--- a/packages/boot/app-boot/src/index.ts
+++ b/packages/boot/app-boot/src/index.ts
@@ -7,7 +7,7 @@
*/
import { pathToFileURL } from 'node:url'
-import { readFileSync } from 'node:fs'
+import { readFileSync, statSync } from 'node:fs'
import { inspect, parseEnv } from 'node:util'
import { basename, dirname, isAbsolute, resolve } from 'node:path'
import * as yaml from 'js-yaml'
@@ -108,8 +108,22 @@ export function resolveConfigPath(
}
/**
- * Load the optional gitignored `.env` from `dir`. Missing files fall back to the
- * ambient environment; other read failures are reported through `warn`.
+ * Whether `path` exists and is a regular file. Returns `false` for missing
+ * paths, directories, FIFOs, sockets, and devices — none of which can hold
+ * dotenv content.
+ */
+function isRegularFile(path: string): boolean {
+ try {
+ return statSync(path).isFile()
+ } catch {
+ return false
+ }
+}
+
+/**
+ * Load the optional gitignored `.env` from `dir`. Missing paths and non-regular
+ * files (directories, FIFOs, sockets, devices) fall back to the ambient
+ * environment; read failures on regular files are reported through `warn`.
* @param binName - the diagnostic prefix on the warn line.
* @param dir - the directory whose `.env` to load.
* @param warn - sink for the one-line misconfiguration diagnostic.
@@ -118,13 +132,14 @@ export function loadEnv(
binName: string, dir: string = process.cwd(),
warn: (line: string) => void = line => void process.stderr.write(line),
): void {
+ const path = resolve(dir, '.env')
+ // A directory (or FIFO, socket, device) named `.env` is not a dotenv file;
+ // treat it like a missing file and fall back to the ambient environment.
+ if (!isRegularFile(path)) return
try {
- process.loadEnvFile(resolve(dir, '.env'))
+ process.loadEnvFile(path)
} catch (error) {
- if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') {
- warn(`${binName}: failed to load .env: ${String(error)}\n`)
- }
- // ENOENT (no .env) is fine — rely on the ambient environment.
+ warn(`${binName}: failed to load .env: ${String(error)}\n`)
}
}
@@ -183,7 +198,8 @@ function isBootstrapOnly(name: string): boolean {
* @param dir - the directory whose `.env` to read.
* @param warn - sink for the one-line unreadable-file diagnostic.
* @param home - the resolved Harness home; when `dir` is it, {@link HOME_LAYER_PROXY_NAMES} are accepted.
- * @returns the parsed entries, or `undefined` when the file is absent or unreadable.
+ * @returns the parsed entries, or `undefined` when the path is absent, not a
+ * regular file, or unreadable.
* @throws when the file declares a name {@link isBootstrapOnly} rejects and this layer may not set.
*/
function readEnvLayer(
@@ -191,14 +207,14 @@ function readEnvLayer(
): { path: string; values: Record<string, string> } | undefined {
const path = resolve(dir, '.env')
const isHome = resolve(dir) === home
+ // A directory (or FIFO, socket, device) named `.env` is not a dotenv file;
+ // treat it like a missing file and fall back to the ambient environment.
+ if (!isRegularFile(path)) return undefined
let content: string
try {
content = readFileSync(path, 'utf8')
} catch (error) {
- if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') {
- warn(`${binName}: failed to load .env: ${String(error)}\n`)
- }
- // ENOENT (no .env) is fine — rely on the ambient environment.
+ warn(`${binName}: failed to load .env: ${String(error)}\n`)
return undefined
}
// Parse once so validation and materialization use exactly the same entries.
diff --git a/packages/boot/app-boot/tests/app-boot.spec.ts b/packages/boot/app-boot/tests/app-boot.spec.ts
index f291e45cc6..7503af9b7e 100644
--- a/packages/boot/app-boot/tests/app-boot.spec.ts
+++ b/packages/boot/app-boot/tests/app-boot.spec.ts
@@ -1,4 +1,4 @@
-import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { chmodSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve, sep } from 'node:path'
import { pathToFileURL } from 'node:url'
@@ -59,11 +59,26 @@ describe('loadEnv', () => {
expect(warn).not.toHaveBeenCalled()
})
- it('warns (labelled, single line) when .env exists but cannot be loaded', () => {
+ it('stays silent when .env is a directory (not a dotenv file)', () => {
const dir = tmp()
- mkdirSync(join(dir, '.env')) // a directory named .env: present, unreadable as a file
+ mkdirSync(join(dir, '.env'))
const warn = vi.fn()
loadEnv(NAME, dir, warn)
+ expect(warn).not.toHaveBeenCalled()
+ })
+
+ it('warns (labelled, single line) when a regular .env cannot be loaded', () => {
+ const dir = tmp()
+ writeFileSync(join(dir, '.env'), 'KEY=value\n')
+ const warn = vi.fn()
+ const load = vi.spyOn(process, 'loadEnvFile').mockImplementation(() => {
+ throw new Error('mock read failure')
+ })
+ try {
+ loadEnv(NAME, dir, warn)
+ } finally {
+ load.mockRestore()
+ }
expect(warn).toHaveBeenCalledTimes(1)
expect(warn.mock.calls[0]?.[0]).toMatch(new RegExp(`^${NAME}: failed to load \\.env: `))
})
@@ -80,17 +95,22 @@ describe('loadEnv', () => {
}
expect(process.env['DSH_APP_BOOT_SPEC_DEFAULTS']).toBe('yes')
delete process.env['DSH_APP_BOOT_SPEC_DEFAULTS']
- // The default warn sink itself: point it at a broken .env with stderr
- // spied, so the arrow body runs without polluting the test output.
+ // The default warn sink itself: spy on loadEnvFile to force a read
+ // failure on a regular file, so the arrow body runs without polluting
+ // the test output.
const broken = tmp()
- mkdirSync(join(broken, '.env'))
+ writeFileSync(join(broken, '.env'), 'KEY=value\n')
const write = vi.spyOn(process.stderr, 'write').mockImplementation(() => true)
+ const load = vi.spyOn(process, 'loadEnvFile').mockImplementation(() => {
+ throw new Error('mock read failure')
+ })
let written: string[]
try {
loadEnv(NAME, broken)
written = write.mock.calls.map(call => String(call[0]))
} finally {
write.mockRestore()
+ load.mockRestore()
}
expect(written).toHaveLength(1)
expect(written[0]).toContain(`${NAME}: failed to load .env: `)
@@ -271,15 +291,39 @@ describe('loadLayeredEnv', () => {
}
})
- it('warns and continues when a layer exists but cannot be read', () => {
+ it('skips a directory layer silently and continues', () => {
const home = tmp()
const project = tmp()
- // A directory named `.env` is a present-but-unreadable layer.
+ // A directory named `.env` is not a dotenv file; it is skipped like a
+ // missing layer.
mkdirSync(join(home, '.env'))
writeFileSync(join(project, '.env'), `${NAMES[2]}=project-only\n`)
clear()
vi.stubEnv('DSH_HOME', home)
const warn = vi.fn()
+ try {
+ const snapshot = loadLayeredEnv(NAME, project, warn)
+ expect(warn).not.toHaveBeenCalled()
+ expect(snapshot.get(NAMES[1])).toBeUndefined()
+ expect(snapshot.get(NAMES[2])).toEqual({ value: 'project-only', source: 'project-env', path: join(project, '.env') })
+ expect(process.env[NAMES[2]]).toBe('project-only')
+ } finally {
+ clear()
+ vi.unstubAllEnvs()
+ }
+ })
+
+ it('warns and continues when a regular layer cannot be read', () => {
+ if (process.platform === 'win32') return // chmod has no effect on Windows
+ const home = tmp()
+ const project = tmp()
+ const homeEnv = join(home, '.env')
+ writeFileSync(homeEnv, `${NAMES[1]}=user-only\n`)
+ writeFileSync(join(project, '.env'), `${NAMES[2]}=project-only\n`)
+ chmodSync(homeEnv, 0o000)
+ clear()
+ vi.stubEnv('DSH_HOME', home)
+ const warn = vi.fn()
try {
const snapshot = loadLayeredEnv(NAME, project, warn)
expect(warn).toHaveBeenCalledWith(expect.stringContaining(`${NAME}: failed to load .env`))
@@ -287,16 +331,20 @@ describe('loadLayeredEnv', () => {
expect(snapshot.get(NAMES[2])).toEqual({ value: 'project-only', source: 'project-env', path: join(project, '.env') })
expect(process.env[NAMES[2]]).toBe('project-only')
} finally {
+ chmodSync(homeEnv, 0o644)
clear()
vi.unstubAllEnvs()
}
})
- it('reports to stderr when the caller supplies no reporter', () => {
+ it('reports to stderr when a regular layer cannot be read and the caller supplies no reporter', () => {
+ if (process.platform === 'win32') return // chmod has no effect on Windows
const home = tmp()
const project = tmp()
- mkdirSync(join(home, '.env'))
+ const homeEnv = join(home, '.env')
+ writeFileSync(homeEnv, `${NAMES[1]}=user-only\n`)
writeFileSync(join(project, '.env'), `${NAMES[2]}=project-only\n`)
+ chmodSync(homeEnv, 0o000)
clear()
vi.stubEnv('DSH_HOME', home)
const write = vi.spyOn(process.stderr, 'write').mockReturnValue(true)
@@ -307,6 +355,7 @@ describe('loadLayeredEnv', () => {
expect(process.env[NAMES[2]]).toBe('project-only')
} finally {
write.mockRestore()
+ chmodSync(homeEnv, 0o644)
clear()
vi.unstubAllEnvs()
}
--
2.54.0 (Apple Git-157) |
Uh oh!
There was an error while loading. Please reload this page.
Environment
@deepseek-ai/dsh: 0.1.0-rc.6 (installed vianpm install -g @deepseek-ai/dsh; also reproduced vianpx @deepseek-ai/dsh web)@deepseek-ai/dsh-app-boot: 0.1.0-rc.6Summary
When a directory named
.envexists in the directory wheredshis invoked (or in the Harness home,~/.dsh), every boot prints:A directory can never be a valid dotenv file, so according to the documented semantics ("missing files fall back to the ambient environment", see the doc comments on
readEnvLayer/loadEnv) this case should be treated like a missing file and skipped silently — not reported as a misconfiguration. The message is non-fatal (the profile still boots), but it reads like a hard error and is confusing.Steps to reproduce
Create an empty directory named
.envin the current directory:Boot any profile from that directory:
dsh --profile web(In my case the
.envdirectory was at the home directoryC:\Users\<user>\.env, anddshwas run from that same directory, which is how the invoking-directory layer picked it up.)Expected behavior
No
.envdiagnostics at all — the directory is not a dotenv file, so the loader falls back to the ambient environment, and boot proceeds cleanly (dsh web: http://127.0.0.1:3080, etc.).Actual behavior
The boot continues afterwards (the line is a warning, not a fatal error).
Root cause
In
packages/boot/app-boot(@deepseek-ai/dsh-app-boot), both.envreaders only treatENOENTas "no file", so any other read error is reported:readEnvLayer()—readFileSync(resolve(dir, ".env"), "utf8"); the catch warns unlesserror?.code !== "ENOENT". This is the function that produced the reported line:apps/cli'sbin.jscallsloadLayeredEnv("dsh"), which reads the invoking-directory layer and the~/.dshuser layer throughreadEnvLayer. In the published package this islib/index.jsaround lines 695–713.loadEnv()—process.loadEnvFile(resolve(dir, ".env")); sameENOENT-only filter. Publishedlib/index.jsaround lines 611–617.When the resolved path is a directory,
fs.readFileSyncthrowsEISDIR(verified standalone:EISDIR: illegal operation on a directory, read), which does not match theENOENTfilter and is therefore printed as "failed to load .env". (process.loadEnvFileon a directory throwsERR_INVALID_ARG_TYPEon Node 24 — a different code, but the same bug: a non-file.envis reported instead of skipped.)Suggested fix
Treat a non-file
.envas "no env file" in both readers, e.g.:or, more robustly, check before reading:
Additional context
.envis a legal folder name; here an emptyC:\Users\<user>\.envdirectory existed in the home directory.cwd/.envand~/.dsh/.env) go through the samereadEnvLayerguard, so the same fix covers both.All reactions