MCPcopy
hub / github.com/claude-code-best/claude-code / getSessionEnvironmentScript

Function getSessionEnvironmentScript

src/utils/sessionEnvironment.ts:60–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

58}
59
60export async function getSessionEnvironmentScript(): Promise<string | null> {
61 if (getPlatform() === 'windows') {
62 logForDebugging('Session environment not yet supported on Windows')
63 return null
64 }
65
66 if (sessionEnvScript !== undefined) {
67 return sessionEnvScript
68 }
69
70 const scripts: string[] = []
71
72 // Check for CLAUDE_ENV_FILE passed from parent process (e.g., HFI trajectory runner)
73 // This allows venv/conda activation to persist across shell commands
74 const envFile = process.env.CLAUDE_ENV_FILE
75 if (envFile) {
76 try {
77 const envScript = (await readFile(envFile, 'utf8')).trim()
78 if (envScript) {
79 scripts.push(envScript)
80 logForDebugging(
81 `Session environment loaded from CLAUDE_ENV_FILE: ${envFile} (${envScript.length} chars)`,
82 )
83 }
84 } catch (e: unknown) {
85 const code = getErrnoCode(e)
86 if (code !== 'ENOENT') {
87 logForDebugging(`Failed to read CLAUDE_ENV_FILE: ${errorMessage(e)}`)
88 }
89 }
90 }
91
92 // Load hook environment files from session directory
93 const sessionEnvDir = await getSessionEnvDirPath()
94 try {
95 const files = await readdir(sessionEnvDir)
96 // We are sorting the hook env files by the order in which they are listed
97 // in the settings.json file so that the resulting env is deterministic
98 const hookFiles = files
99 .filter(f => HOOK_ENV_REGEX.test(f))
100 .sort(sortHookEnvFiles)
101
102 for (const file of hookFiles) {
103 const filePath = join(sessionEnvDir, file)
104 try {
105 const content = (await readFile(filePath, 'utf8')).trim()
106 if (content) {
107 scripts.push(content)
108 }
109 } catch (e: unknown) {
110 const code = getErrnoCode(e)
111 if (code !== 'ENOENT') {
112 logForDebugging(
113 `Failed to read hook file ${filePath}: ${errorMessage(e)}`,
114 )
115 }
116 }
117 }

Callers 1

buildExecCommandFunction · 0.85

Calls 8

getPlatformFunction · 0.85
readFileFunction · 0.85
getErrnoCodeFunction · 0.85
getSessionEnvDirPathFunction · 0.85
readdirFunction · 0.85
logForDebuggingFunction · 0.70
errorMessageFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected