-
Notifications
You must be signed in to change notification settings - Fork 1
fix(core): resolve config outside the working directory #59
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
Open
ffflorian
wants to merge
2
commits into
main
Choose a base branch
from
fix/process-dir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /** | ||
| * Tests for ConfigManager discovery and write-target behaviour | ||
| */ | ||
|
|
||
| import { describe, test, expect, beforeEach, afterEach } from "vitest"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { tmpdir } from "node:os"; | ||
| import { ConfigManager } from "../config/manager.js"; | ||
| import { | ||
| CONFIG_DIR, | ||
| CONFIG_FILENAME, | ||
| CONFIG_SUBDIR_ENV, | ||
| PROJECT_DIR_ENV, | ||
| } from "../types.js"; | ||
|
|
||
| describe("ConfigManager", () => { | ||
| let tempDir: string; | ||
| let homeDir: string; | ||
| let originalEnv: Record<string, string | undefined>; | ||
|
|
||
| async function writeConfig( | ||
| directory: string, | ||
| docsetId: string, | ||
| ): Promise<string> { | ||
| const knowledgeDir = join(directory, CONFIG_DIR); | ||
| await fs.mkdir(knowledgeDir, { recursive: true }); | ||
| const configPath = join(knowledgeDir, CONFIG_FILENAME); | ||
| await fs.writeFile( | ||
| configPath, | ||
| [ | ||
| 'version: "1.0"', | ||
| "docsets:", | ||
| ` - id: ${docsetId}`, | ||
| ` name: ${docsetId}`, | ||
| " sources:", | ||
| " - type: git_repo", | ||
| " url: https://example.com/repo.git", | ||
| " branch: main", | ||
| ].join("\n"), | ||
| ); | ||
| return configPath; | ||
| } | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = await fs.mkdtemp(join(tmpdir(), "agentic-knowledge-manager-")); | ||
| homeDir = join(tempDir, "home"); | ||
| await fs.mkdir(homeDir, { recursive: true }); | ||
| originalEnv = { | ||
| HOME: process.env.HOME, | ||
| USERPROFILE: process.env.USERPROFILE, | ||
| [CONFIG_SUBDIR_ENV]: process.env[CONFIG_SUBDIR_ENV], | ||
| [PROJECT_DIR_ENV]: process.env[PROJECT_DIR_ENV], | ||
| }; | ||
| process.env.HOME = homeDir; | ||
| process.env.USERPROFILE = homeDir; | ||
| delete process.env[CONFIG_SUBDIR_ENV]; | ||
| delete process.env[PROJECT_DIR_ENV]; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| for (const [key, value] of Object.entries(originalEnv)) { | ||
| if (value === undefined) { | ||
| delete process.env[key]; | ||
| } else { | ||
| process.env[key] = value; | ||
| } | ||
| } | ||
| await fs.rm(tempDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| test("caches per lookup, not per instance", async () => { | ||
| const projectA = join(tempDir, "a"); | ||
| const projectB = join(tempDir, "b"); | ||
| await fs.mkdir(projectA, { recursive: true }); | ||
| await fs.mkdir(projectB, { recursive: true }); | ||
| const configA = await writeConfig(projectA, "a"); | ||
| const configB = await writeConfig(projectB, "b"); | ||
|
|
||
| const manager = new ConfigManager(); | ||
| expect((await manager.loadConfig(projectA)).configPath).toBe(configA); | ||
| expect((await manager.loadConfig(projectB)).configPath).toBe(configB); | ||
| }); | ||
|
|
||
| test("does not share a result between includeHome variants", async () => { | ||
| const project = join(tempDir, "project"); | ||
| await fs.mkdir(project, { recursive: true }); | ||
| const homeConfig = await writeConfig(homeDir, "global"); | ||
|
|
||
| expect( | ||
| (await new ConfigManager().loadConfig(project, { includeHome: true })) | ||
| .configPath, | ||
| ).toBe(homeConfig); | ||
|
|
||
| const manager = new ConfigManager(); | ||
| await manager.loadConfig(project, { includeHome: true }); | ||
| await expect(manager.loadConfig(project)).rejects.toThrow( | ||
| /No configuration file found/, | ||
| ); | ||
| }); | ||
|
|
||
| test("updateDocsetPaths writes to the config it was given", async () => { | ||
| const project = join(tempDir, "project"); | ||
| await fs.mkdir(project, { recursive: true }); | ||
| const projectConfig = await writeConfig(project, "docs"); | ||
| await writeConfig(homeDir, "docs"); | ||
|
|
||
| const manager = new ConfigManager(); | ||
| await manager.updateDocsetPaths("docs", ["docs/"], projectConfig); | ||
|
|
||
| const written = await fs.readFile(projectConfig, "utf-8"); | ||
| expect(written).toContain("docs/"); | ||
| const homeContent = await fs.readFile( | ||
| join(homeDir, CONFIG_DIR, CONFIG_FILENAME), | ||
| "utf-8", | ||
| ); | ||
| expect(homeContent).not.toContain("docs/"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guard is exactly right for
create. Butinitwrites to the config too —ensureKnowledgeGitignoreSync(configPath),coreInitDocset(..., configPath, ...)(drops metadata into the.knowledgedir), andupdateDocsetPaths(...)(rewritesconfig.yaml) — all reached viaconfigManager.loadConfig(cwd)ininit.tswith noincludeHomeoption. So a project with no local config but a matching docset in~/.knowledge/config.yamlwill haveinitwrite into the home config — the same class of bug this fixes here. Suggest extending the guard toinit(and reconsideringrefresh, which also callsensureKnowledgeGitignoreSync), or documenting why init-to-home is intended.