Unity-Technologies/UnityCsReference · error · ArgumentException

Path cannot be null or empty.

Error message

Path cannot be null or empty.

What it means

FileUtil.OpenRead (the newer Stream-returning overload in FileUtil.cs) guards its path argument with string.IsNullOrEmpty and throws ArgumentException if either holds. Unlike the bindings-layer Delete/Copy guards, this also rejects empty uniformly with null because the subsequent GetPhysicalPath and VFS lookup cannot proceed on an absent path.

Source

Thrown at Editor/Mono/FileUtil.cs:26

using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Bindings;
using UnityEngine.Scripting;

namespace UnityEditor
{
    public partial class FileUtil
    {
        const string k_UdsPathPrefix = "uds:/";

        /// Opens a file for reading. For UDS paths (uds:/...), returns a zero-copy
        /// stream backed by native shared memory. For physical paths, returns a
        /// standard FileStream. For other VFS paths, materialises via native read
        public static Stream OpenRead(string path)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentException("Path cannot be null or empty.", nameof(path));

            var physicalPath = FileUtil.GetPhysicalPath(path);

            if (string.IsNullOrEmpty(physicalPath))
            {
                // Path exists in VFS but not on the physical filesystem —
                // materialise the data through the native VFS read
                byte[] data = ReadAllBytesInternal(path);
                return new MemoryStream(data, writable: false);
            }

            // The 'physical' path for a VirtualArtifact will be a UDS virtual path
            if (physicalPath.StartsWith(k_UdsPathPrefix, StringComparison.Ordinal))
                return OpenReadUDS(physicalPath);

            // Fall back to opening the physical file in the normal way
            return File.Open(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Validate the path with string.IsNullOrEmpty (or IsNullOrWhiteSpace) before opening.
  2. Where the path is computed, ensure each component (base dir, relative) is non-empty.
  3. Provide a clear early error naming the missing field rather than letting the ArgumentException bubble.

Example fix

// before
using Stream s = FileUtil.OpenRead(path);
// after
if (string.IsNullOrEmpty(path)) throw new ArgumentException("path required", nameof(path));
using Stream s = FileUtil.OpenRead(path);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(path))
    throw new ArgumentException("path required", nameof(path));
using Stream s = FileUtil.OpenRead(path);

Type guard

static bool IsReadablePath(string p) => !string.IsNullOrEmpty(p);

Prevention

When it happens

Trigger: Calling FileUtil.OpenRead(null) or FileUtil.OpenRead(""). Passing a path from a lookup (AssetDatabase, config) that resolved to null or empty. Streaming-assets reads where the relative path was concatenated to an unset base.

Common situations: Reading an asset whose path lookup failed; reading a config file whose location was not configured; editor tooling that reads a file chosen via ObjectField that the user cleared.

Related errors


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