Repository: fsspec/filesystem_spec

Issue:
compressed reference files are broken since `fsspec=2023.04.0`
I can't seem to open references stored as compressed JSON files using `fsspec>=2023.04.0`.

## Steps to reproduce
```sh
wget https://github.com/pydata/xarray-data/blob/master/ROMS_example.nc
mamba create -n test python=3.11 ipython fsspec kerchunk h5py ujson zstandard
mamba activate test
```
```python
In [1]: from kerchunk.hdf import SingleHdf5ToZarr
   ...: import fsspec
   ...: import ujson
   ...: 
   ...: refs = SingleHdf5ToZarr("ROMS_example.nc").translate()
   ...: 
   ...: fs = fsspec.filesystem("file")
   ...: with fs.open("references.json.zst", mode="w", compression="zstd") as f:
   ...:     ujson.dump(refs, f)
   ...:
```

### with `fsspec>=2023.04.0`:
```sh
mamba install 'fsspec=2023.04.0'
```
```python
In [1]: import fsspec
   ...: 
   ...: fs = fsspec.filesystem("reference", fo="references.json.zst", target_options={"compression": "zstd"})
   ...: m = fs.get_mapper("")
   ...: list(m)
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
Cell In[1], line 3
      1 import fsspec
----> 3 fs = fsspec.filesystem("reference", fo="references.json.zst", target_options={"compression": "zstd"})
      4 m = fs.get_mapper("")
      5 list(m)

File ~/.local/opt/mambaforge/envs/test/lib/python3.11/site-packages/fsspec/registry.py:257, in filesystem(protocol, **storage_options)
    250     warnings.warn(
    251         "The 'arrow_hdfs' protocol has been deprecated and will be "
    252         "removed in the future. Specify it as 'hdfs'.",
    253         DeprecationWarning,
    254     )
    256 cls = get_filesystem_class(protocol)
--> 257 return cls(**storage_options)

File ~/.local/opt/mambaforge/envs/test/lib/python3.11/site-packages/fsspec/spec.py:76, in _Cached.__call__(cls, *args, **kwargs)
     74     return cls._cache[token]
     75 else:
---> 76     obj = super().__call__(*args, **kwargs)
     77     # Setting _fs_token here causes some static linters to complain.
     78     obj._fs_token_ = token

File ~/.local/opt/mambaforge/envs/test/lib/python3.11/site-packages/fsspec/implementations/reference.py:376, in ReferenceFileSystem.__init__(self, fo, target, ref_storage_args, target_protocol, target_options, remote_protocol, remote_options, fs, template_overrides, simple_templates, max_gap, max_block, cache_size, **kwargs)
    374     with ref_fs.open(fo, "rb") as f:
    375         logger.info("Read reference from URL %s", fo)
--> 376         text = json.load(f)
    377     self._process_references(text, template_overrides)
    378 else:
    379     # Lazy parquet refs

JSONDecodeError: Expected object or value
```

### With `fsspec=2023.03.0`:
```sh
mamba install 'fsspec=2023.03.0'
```
```python
In [1]: import fsspec
   ...: 
   ...: fs = fsspec.filesystem("reference", fo="references.json.zst", target_options={"compression": "zstd"})
   ...: m = fs.get_mapper("")
   ...: list(m)
Out[1]: 
['.zattrs',
 '.zgroup',
 'Cs_r/.zarray',
 'Cs_r/.zattrs',
 'Cs_r/0',
 'Vtransform/.zarray',
 'Vtransform/.zattrs',
 'Vtransform/0',
 'h/.zarray',
 'h/.zattrs',
 'h/0.0',
 'hc/.zarray',
 'hc/.zattrs',
 'hc/0',
 'lat_rho/.zarray',
 'lat_rho/.zattrs',
 'lat_rho/0.0',
 'lon_rho/.zarray',
 'lon_rho/.zattrs',
 'lon_rho/0.0',
 'ocean_time/.zarray',
 'ocean_time/.zattrs',
 'ocean_time/0',
 's_rho/.zarray',
 's_rho/.zattrs',
 's_rho/0',
 'salt/.zarray',
 'salt/.zattrs',
 'salt/0.0.0.0',
 'salt/0.0.0.1',
 'salt/0.0.1.0',
 'salt/0.0.1.1',
 'salt/0.1.0.0',
 'salt/0.1.0.1',
 'salt/0.1.1.0',
 'salt/0.1.1.1',
 'salt/1.0.0.0',
 'salt/1.0.0.1',
 'salt/1.0.1.0',
 'salt/1.0.1.1',
 'salt/1.1.0.0',
 'salt/1.1.0.1',
 'salt/1.1.1.0',
 'salt/1.1.1.1',
 'zeta/.zarray',
 'zeta/.zattrs',
 'zeta/0.0.0',
 'zeta/1.0.0']
```

Discussion:

Patch:
diff --git a/fsspec/implementations/reference.py b/fsspec/implementations/reference.py
index a17143a..f93fe45 100644
--- a/fsspec/implementations/reference.py
+++ b/fsspec/implementations/reference.py
@@ -620,17 +620,18 @@ class ReferenceFileSystem(AsyncFileSystem):
             dic = dict(
                 **(ref_storage_args or target_options or {}), protocol=target_protocol
             )
-            ref_fs, fo = fsspec.core.url_to_fs(fo, **dic)
+            ref_fs, fo2 = fsspec.core.url_to_fs(fo, **dic)
             if ref_fs.isfile(fo):
                 # text JSON
-                with ref_fs.open(fo, "rb") as f:
+                with fsspec.open(fo, "rb", **dic) as f:
                     logger.info("Read reference from URL %s", fo)
                     text = json.load(f)
                 self._process_references(text, template_overrides)
             else:
                 # Lazy parquet refs
+                logger.info("Open lazy reference dict from URL %s", fo)
                 self.references = LazyReferenceMapper(
-                    fo,
+                    fo2,
                     fs=ref_fs,
                     cache_size=cache_size,
                 )


Test patch:
diff --git a/fsspec/implementations/tests/test_reference.py b/fsspec/implementations/tests/test_reference.py
index 540fcbc..af29caa 100644
--- a/fsspec/implementations/tests/test_reference.py
+++ b/fsspec/implementations/tests/test_reference.py
@@ -28,6 +28,17 @@ def test_simple(server):  # noqa: F811
         assert f.read(2) == "he"
 
 
+def test_target_options(m):
+    m.pipe("data/0", b"hello")
+    refs = {"a": ["memory://data/0"]}
+    fn = "memory://refs.json.gz"
+    with fsspec.open(fn, "wt", compression="gzip") as f:
+        json.dump(refs, f)
+
+    fs = fsspec.filesystem("reference", fo=fn, target_options={"compression": "gzip"})
+    assert fs.cat("a") == b"hello"
+
+
 def test_ls(server):  # noqa: F811
     refs = {"a": b"data", "b": (realfile, 0, 5), "c/d": (realfile, 1, 6)}
     h = fsspec.filesystem("http")