{"record":{"id":"75decd4be17b79a0","repo":"astral-sh/uv","slug":"cannot-use-as-a-constraint-file","errorCode":null,"errorMessage":"Cannot use `{}` as a constraint file","messagePattern":"Cannot use `(.+?)` as a constraint file","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/uv-requirements/src/specification.rs","lineNumber":426,"sourceCode":"        requirements: &[RequirementsSource],\n        constraints: &[RequirementsSource],\n        overrides: &[RequirementsSource],\n        excludes: &[RequirementsSource],\n        groups: Option<&GroupsSpecification>,\n        client_builder: &BaseClientBuilder<'_>,\n    ) -> Result<Self> {\n        let mut spec = Self::default();\n        let mut cache = SourceCache::default();\n\n        // Disallow `pylock.toml` files as constraints.\n        if let Some(pylock_toml) = constraints.iter().find_map(|source| {\n            if let RequirementsSource::PylockToml(path) = source {\n                Some(path)\n            } else {\n                None\n            }\n        }) {\n            return Err(anyhow::anyhow!(\n                \"Cannot use `{}` as a constraint file\",\n                pylock_toml.user_display()\n            ));\n        }\n\n        // Disallow `pylock.toml` files as overrides.\n        if let Some(pylock_toml) = overrides.iter().find_map(|source| {\n            if let RequirementsSource::PylockToml(path) = source {\n                Some(path)\n            } else {\n                None\n            }\n        }) {\n            return Err(anyhow::anyhow!(\n                \"Cannot use `{}` as an override file\",\n                pylock_toml.user_display()\n            ));\n        }","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/astral-sh/uv/blob/f1a42680ff5272232d65748acf338b19778dde24/crates/uv-requirements/src/specification.rs#L408-L444","documentation":"Thrown in RequirementsSpecification::from_sources (crates/uv-requirements/src/specification.rs:426) when any source in the constraints list is already a RequirementsSource::PylockToml variant. This is the aggregation-level guard that complements error 40: even when sources were constructed through a path that bypassed from_constraints_txt's file-name sniffing, a pylock.toml can never act as a constraint file.","triggerScenarios":"Programmatically calling from_sources with a PylockToml source in the constraints Vec; CLI flows where a constraint path was resolved to a pylock source upstream (e.g. `--constraints pylock.toml` reaching this constructor); only the first offending path is reported.","commonSituations":"Wrapper tools building RequirementsSpecification directly from mixed source lists; scripts that glob *.toml into constraint paths; lock-and-constrain hybrid CI setups.","solutions":["Remove the pylock.toml entry from the constraints list — constraints must be requirements.txt-format sources","Export the lock to requirements format and use that as the constraint source","If the intent was exact-lock installation, move pylock.toml into requirements (`-r pylock.toml`) and drop --constraints entirely (see error 57)"],"exampleFix":"# before (programmatic)\nlet spec = RequirementsSpecification::from_sources(\n    vec![RequirementsSource::PylockToml(lock.into())], /* ... */\n// after\nlet spec = RequirementsSpecification::from_sources(\n    vec![RequirementsSource::RequirementsTxt(constraints.into())], /* ... */","handlingStrategy":"validation","validationCode":"let bad = constraints.iter().any(|s| matches!(s, RequirementsSource::PylockToml(_)));\nif bad {\n    return Err(anyhow::anyhow!(\"refusing to assemble spec: pylock.toml is not a constraint source\"));\n}","typeGuard":"fn is_pylock_source(s: &RequirementsSource) -> bool {\n    matches!(s, RequirementsSource::PylockToml(_))\n}","tryCatchPattern":"match RequirementsSpecification::from_sources(requirements, constraints, overrides, /* .. */).await {\n    Err(err) if err.to_string().contains(\"as a constraint file\") => {\n        // strip pylock sources from constraints and reassemble\n    }\n    spec => spec?,\n}","preventionTips":["Type your source lists by role (constraints = requirements.txt format only)","Validate assembled lists with matches! guards before calling from_sources","Keep lock files out of glob-derived constraint lists"],"tags":["uv","constraints","pylock-toml","api-misuse"],"backgroundTag":null,"analyzedSha":"f1a42680ff5272232d65748acf338b19778dde24","analyzedAt":"2026-08-16T04:51:47.599Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}