{"record":{"id":"5be6e7513017f312","repo":"chocolatey/choco","slug":"package-name-cannot-be-a-path-to-a-file","errorCode":null,"errorMessage":"Package name cannot be a path to a file","messagePattern":"Package name cannot be a path to a file","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs","lineNumber":1373,"sourceCode":"                sb.AppendLine(\"on a UNC location.\")\r\n                  .AppendLine()\r\n                  .Append(\"To \")\r\n                  .Append(commandName.ToLowerSafe())\r\n                  .AppendLine(\" a file in a UNC location, you may use:\");\r\n            }\r\n            else\r\n            {\r\n                sb.AppendLine(\"on a remote, or local file system.\")\r\n                  .AppendLine()\r\n                  .Append(\"To \")\r\n                  .Append(commandName.ToLowerSafe())\r\n                  .AppendLine(\" a local, or remote file, you may use:\");\r\n\r\n            }\r\n\r\n            BuildInstallExample(packageName, sb, commandName.ToLowerSafe());\r\n\r\n            throw new ApplicationException(sb.AppendLine().ToString());\r\n        }\r\n\r\n        private void BuildInstallExample(string packageName, StringBuilder sb, string commandName)\r\n        {\r\n            var fileName = _fileSystem.GetFilenameWithoutExtension(packageName);\r\n            var version = string.Empty;\r\n            // We need to get the directory name in this way in case it is a UNC path.\r\n            // Using normal way to get the directory name may trim out parts of the necessary path.\r\n            var length = packageName.Length - _fileSystem.GetFileName(packageName).Length - 1;\r\n            var directory = length > 0 ? packageName.Substring(0, length) : string.Empty;\r\n\r\n            if (fileName.Contains('.'))\r\n            {\r\n                var originalFileName = fileName;\r\n                fileName = string.Empty;\r\n\r\n                while (fileName.Length < originalFileName.Length)\r\n                {\r","sourceCodeStart":1355,"sourceCodeEnd":1391,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs#L1355-L1391","documentation":"Thrown as an ApplicationException from ThrowInvalidPathError when a package name ending in '.nupkg' resolves to a valid local file or UNC path. Unlike error 68 (where the file does not exist), this fires when the file IS found — the user is pointing to a specific .nupkg file instead of using --source. The message includes a contextual usage example built by BuildInstallExample showing the correct choco command syntax.","triggerScenarios":"ValidatePackageNames detects a .nupkg package name that Uri.TryCreate resolves to a file:// or UNC URI, or that _fileSystem.FileExists confirms exists. ThrowInvalidPathError is called with the resolved path and UNC flag, building a detailed message with a correct usage example before throwing.","commonSituations":"A user runs 'choco install C:\\downloads\\firefox.nupkg' or 'choco install \\\\server\\share\\firefox.nupkg'. Chocolatey identifies the file path but rejects it, instead showing the correct command: 'choco install firefox --source=C:\\downloads'.","solutions":["Follow the generated example in the error message — it shows the exact correct command for your scenario.","Use --source to point to the directory containing the .nupkg and pass the package name without path or extension.","For UNC paths: 'choco install firefox --source=\\\\server\\share\\packages'.","For local paths: 'choco install firefox --source=C:\\downloads'."],"exampleFix":"// before: pointing package name at a specific .nupkg file\nchoco install C:\\downloads\\firefox.1.0.0.nupkg\n\n// after: use --source for the directory, package name only\nchoco install firefox --source=C:\\downloads\n// the error message itself generates this example dynamically","handlingStrategy":"validation","validationCode":"// Detect file-path package names and redirect before the API throws\nforeach (var name in config.PackageNames.Split(';'))\n{\n    if (name.EndsWith(\".nupkg\", StringComparison.OrdinalIgnoreCase))\n    {\n        if (Uri.TryCreate(name, UriKind.Absolute, out var uri) && (uri.IsFile || uri.IsUnc))\n        {\n            var dir = Path.GetDirectoryName(uri.LocalPath);\n            var pkgId = Path.GetFileNameWithoutExtension(uri.LocalPath);\n            throw new InvalidOperationException($\"Use: choco install {pkgId} --source={dir}\");\n        }\n        if (_fileSystem.FileExists(name))\n        {\n            var dir = Path.GetDirectoryName(_fileSystem.GetFullPath(name));\n            var pkgId = _fileSystem.GetFilenameWithoutExtension(name);\n            throw new InvalidOperationException($\"Use: choco install {pkgId} --source={dir}\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    _packageService.Install(config);\n}\ncatch (ApplicationException ex) when (ex.Message.Contains(\"Package name cannot be a path to a file\"))\n{\n    // The error message already contains a generated usage example\n    logger.Error(ex.Message); // shows the correct 'choco install ... --source=...' command\n}","preventionTips":["Always use --source to specify the directory containing .nupkg files, never the file path itself.","For UNC paths, format as: choco install pkgname --source=\\\\server\\share\\dir.","The error message itself generates the correct command — read it and follow the suggestion."],"tags":["package-name","validation","nupkg","file-path","unc","source-argument","chocolatey"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}