Unity-Technologies/UnityCsReference · error · ArgumentNullException
Cannot add dependency on invalid path.
Error message
Cannot add dependency on invalid path.
What it means
AssetImportContext.DependsOnSourceAsset(string path) registers a source-level dependency: if the asset at the given path changes or moves, the importing asset is re-imported. It throws ArgumentNullException with message 'Cannot add dependency on invalid path.' when path is null or empty, because an empty path cannot resolve to a valid source asset and would silently create a dead dependency.
Source
Thrown at Editor/Mono/AssetPipeline/AssetImportContext.bindings.cs:65
public void AddObjectToAsset(string identifier, Object obj)
{
AddObjectToAsset(identifier, obj, null);
}
[FreeFunction("AssetImportContextBindings::GetObjects", HasExplicitThis = true)]
public extern void GetObjects([NotNull][Out] List<Object> objects);
[NativeMethod(ThrowsException = true)]
public extern void AddObjectToAsset(string identifier, Object obj, Texture2D thumbnail);
// Create a dependency against the contents of the source asset at the provided path
// * if the asset at the path changes, it will trigger an import
// * if the asset at the path moves, it will trigger an import
public void DependsOnSourceAsset(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path", "Cannot add dependency on invalid path.");
}
DependsOnSourceAssetInternal(path);
}
[NativeName("DependsOnSourceAsset")]
private extern void DependsOnSourceAssetInternal(string path);
public void DependsOnSourceAsset(GUID guid)
{
if (guid.Empty())
{
throw new ArgumentNullException("guid", "Cannot add source dependency on empty GUID.");
}
DependsOnSourceAssetInternalGUID(guid);
}
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Validate the path before calling: if (!string.IsNullOrEmpty(depPath)) context.DependsOnSourceAsset(depPath).
- Log a warning and skip the dependency if the source file references an empty path, treating it as optional or malformed input.
- Ensure dependency paths are resolved through AssetDatabase.GUIDToAssetPath with GUID validation first.
Example fix
// before
context.DependsOnSourceAsset(referencedPath);
// after
if (!string.IsNullOrEmpty(referencedPath))
context.DependsOnSourceAsset(referencedPath);
else
Debug.LogWarning($"{context.assetPath}: skipping empty source dependency"); Defensive patterns
Strategy: validation
Validate before calling
if (!string.IsNullOrEmpty(depPath))
context.DependsOnSourceAsset(depPath);
else
Debug.LogWarning($"{context.assetPath}: empty source dependency path"); Type guard
static bool IsValidDependencyPath(string path) => !string.IsNullOrEmpty(path);
Try / catch
try { context.DependsOnSourceAsset(path); }
catch (ArgumentNullException ex) when (ex.ParamName == "path")
{ Debug.LogWarning($"Skipped source dependency: {ex.Message}"); } Prevention
- Validate all dependency paths in OnImportAsset before registering them
- Resolve paths via AssetDatabase.GUIDToAssetPath and check for empty
- Log skipped dependencies to catch malformed source-file references early
When it happens
Trigger: Calling context.DependsOnSourceAsset(null) or context.DependsOnSourceAsset("") from within a ScriptedImporter's OnImportAsset method. The path typically comes from a serialized reference, a computed GUID-to-path lookup, or a sub-asset path that can be empty if the source data is malformed.
Common situations: Custom ScriptedImporters that declare dependencies on other assets (textures, shaders, data files) where the dependency path is derived from the imported file's content and can be null/empty if the source file references a missing or unconfigured asset.
Related errors
- Cannot add source dependency on empty GUID.
- Cannot add artifact dependency on empty GUID.
- Cannot add custom dependency on an empty custom dependency.
- Cannot add dependency on invalid ArtifactKey.
- A shader '{assetPath}' cannot depend on the 'srp/default-sha
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/532ecb8a7e8359a8.
Report an issue: GitHub.