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

  1. Validate the path before calling: if (!string.IsNullOrEmpty(depPath)) context.DependsOnSourceAsset(depPath).
  2. Log a warning and skip the dependency if the source file references an empty path, treating it as optional or malformed input.
  3. 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

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


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/532ecb8a7e8359a8. Report an issue: GitHub.