@@ -20,6 +20,7 @@ import * as Layer from "effect/Layer";
2020import * as Path from "effect/Path" ;
2121import * as Schema from "effect/Schema" ;
2222
23+ import * as KnownWorkspaces from "./KnownWorkspaces.ts" ;
2324import * as WorkspaceEntries from "./WorkspaceEntries.ts" ;
2425import * as WorkspacePaths from "./WorkspacePaths.ts" ;
2526import { fileHeaderMimeType } from "./fileHeaderMimeType.ts" ;
@@ -119,11 +120,14 @@ export class WorkspaceFileSystem extends Context.Service<
119120 ProjectReadFileResult ,
120121 WorkspaceFileSystemError | WorkspacePaths . WorkspacePathOutsideRootError
121122 > ;
123+ /** Writes inside a known workspace only. */
122124 readonly writeFile : (
123125 input : ProjectWriteFileInput ,
124126 ) => Effect . Effect <
125127 ProjectWriteFileResult ,
126- WorkspaceFileSystemError | WorkspacePaths . WorkspacePathOutsideRootError
128+ | WorkspaceFileSystemError
129+ | WorkspacePaths . WorkspacePathOutsideRootError
130+ | KnownWorkspaces . UnknownWorkspaceError
127131 > ;
128132 }
129133> ( ) ( "t3/workspace/WorkspaceFileSystem" ) { }
@@ -133,6 +137,7 @@ export const make = Effect.gen(function* () {
133137 const fileSystem = yield * FileSystem . FileSystem ;
134138 const path = yield * Path . Path ;
135139 const workspacePaths = yield * WorkspacePaths . WorkspacePaths ;
140+ const knownWorkspaces = yield * KnownWorkspaces . KnownWorkspaces ;
136141 const workspaceEntries = yield * WorkspaceEntries . WorkspaceEntries ;
137142
138143 const metadataForPath = Effect . fnUntraced ( function * ( requestedPath : string ) {
@@ -192,15 +197,19 @@ export const make = Effect.gen(function* () {
192197 * Git runs config and hooks from its own folders, so a write never lands in one:
193198 * a `.git` entry, or a repository folder under another name such as `.bare`.
194199 * Folders are checked upward to a linked worktree's top, which holds a `.git`
195- * file, since worktrees can sit inside a bare repository. `realPath ` must be a
196- * real path .
200+ * file, since worktrees can sit inside a bare repository. `absolutePath ` must
201+ * be normalized .
197202 */
198- const isInGitFolder = async ( realPath : string ) => {
199- if ( realPath . split ( path . sep ) . some ( isGitMetadataName ) ) return true ;
200- for ( let folder = path . dirname ( realPath ) ; ; folder = path . dirname ( folder ) ) {
203+ const isInGitFolder = async ( absolutePath : string ) => {
204+ if ( absolutePath . split ( path . sep ) . some ( isGitMetadataName ) ) return true ;
205+ for ( let folder = path . dirname ( absolutePath ) ; ; folder = path . dirname ( folder ) ) {
201206 const [ head , commondir , objects , refs , dotGit ] = await Promise . all (
202207 [ "HEAD" , "commondir" , "objects" , "refs" , ".git" ] . map ( ( name ) =>
203- NodeFSP . lstat ( path . join ( folder , name ) ) . catch ( ( ) => null ) ,
208+ NodeFSP . lstat ( path . join ( folder , name ) ) . catch ( ( error : NodeJS . ErrnoException ) => {
209+ // Any other failure leaves the folder unknown, so the write is refused.
210+ if ( error . code === "ENOENT" || error . code === "ENOTDIR" ) return null ;
211+ throw error ;
212+ } ) ,
204213 ) ,
205214 ) ;
206215 // What git reads as a repository: HEAD, plus objects and refs here or in
@@ -424,9 +433,26 @@ export const make = Effect.gen(function* () {
424433 cause,
425434 } ) ,
426435 } ) ;
436+ // The path as named is checked too: a `.git/config` symlink resolves away the `.git`.
437+ const namedTargetPath = path . join ( realWorkspaceRoot , ...segments ) ;
438+ const inGitFolder = Effect . fn ( "WorkspaceFileSystem.inGitFolder" ) ( ( checkedPath : string ) =>
439+ Effect . tryPromise ( {
440+ try : ( ) => isInGitFolder ( checkedPath ) ,
441+ catch : ( cause ) =>
442+ new WorkspaceFileSystemOperationError ( {
443+ workspaceRoot : input . cwd ,
444+ relativePath : input . relativePath ,
445+ resolvedPath : realTargetPath ,
446+ operationPath : checkedPath ,
447+ operation : "stat" ,
448+ cause,
449+ } ) ,
450+ } ) ,
451+ ) ;
427452 if (
428453 ! isInsideRealRoot ( realWorkspaceRoot , realTargetPath ) ||
429- ( yield * Effect . promise ( ( ) => isInGitFolder ( realTargetPath ) ) )
454+ ( yield * inGitFolder ( realTargetPath ) ) ||
455+ ( yield * inGitFolder ( namedTargetPath ) )
430456 ) {
431457 return yield * new WorkspaceFilePathEscapeError ( {
432458 workspaceRoot : input . cwd ,
@@ -441,6 +467,7 @@ export const make = Effect.gen(function* () {
441467 const writeFile : WorkspaceFileSystem [ "Service" ] [ "writeFile" ] = Effect . fn (
442468 "WorkspaceFileSystem.writeFile" ,
443469 ) ( function * ( input ) {
470+ yield * knownWorkspaces . assertContains ( input . cwd ) ;
444471 const target = yield * resolveWriteTarget ( input ) ;
445472 const realTargetPath = target . realTargetPath ;
446473
0 commit comments