{"record":{"id":"195d0eb30a0fc259","repo":"microsoft/aspire","slug":"the-staged-dependency-directory-dependencydirectory-could","errorCode":null,"errorMessage":"The staged dependency directory '{dependencyDirectory}' could not be cleared, so the AppHost would run with both the old and new versions of any upgraded dependency on its classpath. Close anything holding files in that directory, or delete it manually, then run the command again.","messagePattern":"The staged dependency directory '(.+?)' could not be cleared, so the AppHost would run with both the old and new versions of any upgraded dependency on its classpath\\. Close anything holding files in that directory, or delete it manually, then run the command again\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Projects/JavaAppHostToolchainResolver.cs","lineNumber":512,"sourceCode":"\n        var dependencyDirectory = Path.Combine(\n            resolution.ProjectDirectory.FullName,\n            GetDependencyDirectory(resolution.Toolchain));\n\n        try\n        {\n            if (Directory.Exists(dependencyDirectory))\n            {\n                Directory.Delete(dependencyDirectory, recursive: true);\n            }\n        }\n        catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)\n        {\n            // Continuing here would defeat the only reason this method exists. Maven adds the new\n            // versioned JAR beside the stale one, both end up on the \"dir/*\" classpath, and which one\n            // the JVM loads is left to directory order — a failure that surfaces later as an unrelated\n            // NoSuchMethodError. Better to stop now with a message that names the directory.\n            throw new InvalidOperationException(\n                $\"The staged dependency directory '{dependencyDirectory}' could not be cleared, so the \" +\n                \"AppHost would run with both the old and new versions of any upgraded dependency on its \" +\n                \"classpath. Close anything holding files in that directory, or delete it manually, then \" +\n                \"run the command again.\",\n                ex);\n        }\n    }\n\n    /// <summary>\n    /// Path from the AppHost directory to the project directory, or <see langword=\"null\"/> when they are\n    /// the same. Null rather than \".\" so the common flat layout keeps clean, unprefixed relative paths.\n    /// </summary>\n    private static string? GetRelativeProjectPath(DirectoryInfo projectDirectory, DirectoryInfo appHostDirectory)\n    {\n        var relativePath = Path.GetRelativePath(appHostDirectory.FullName, projectDirectory.FullName);\n\n        return relativePath == \".\" ? null : relativePath;\n    }","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Projects/JavaAppHostToolchainResolver.cs#L494-L530","documentation":"Before running an upgraded Java AppHost, ClearStagedDependencies wipes the staged dependency directory. If deletion fails with an IOException or UnauthorizedAccessException, the method throws rather than proceeding, because leaving stale JARs would put both old and new versions of an upgraded dependency on the classpath and cause hard-to-diagnose load-order bugs.","triggerScenarios":"Calling ClearStagedDependencies while another process (running AppHost, IDE, file indexer) holds files open in the dependency directory, or when the current user lacks delete permission on it.","commonSituations":"A previously launched AppHost still running and locking JARs (Windows), an IDE or antivirus scanning the directory, or read-only permissions after copying artifacts.","solutions":["Stop any running AppHost or process that could hold files in the staged dependency directory, then rerun the command","Delete the named directory manually and rerun","Check/fix permissions on the directory (chmod or take ownership) if deletion was denied"],"exampleFix":"// before\n$ aspire run  # error: staged dir locked by running AppHost\n// after\n$ kill <apphost pid>   # or close the IDE-launched run\n$ rm -rf .aspire/staged/deps\n$ aspire run","handlingStrategy":"retry","validationCode":"// Best-effort: ensure no live process pins the directory before invoking\nvar hasLocks = Directory.EnumerateFiles(dependencyDirectory, \"*\", SearchOption.AllDirectories)\n    .Any(f => { try { using var s = File.Open(f, FileMode.Open, FileAccess.Read, FileShare.None); return false; } catch (IOException) { return true; } });","typeGuard":null,"tryCatchPattern":"try { resolver.ClearStagedDependencies(dir); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"could not be cleared\"))\n{\n    // stop running AppHosts, retry after delay\n    await Task.Delay(1000); retryOnce = true;\n}","preventionTips":["Stop running AppHosts before upgrading/re-running commands that restage dependencies","Exclude staging directories from antivirus/indexers where possible","Run CLI commands as a user with write/delete access to the staging directory"],"tags":["java","filesystem","file-locked","permissions","dependencies"],"backgroundTag":"file-delete-failed","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}