summary refs log tree commit diff
path: root/pkgs/misc/vscode-extensions/ms-dotnettools-csharp/default.nix
blob: d91cbccb8083200dd0a0bba70e882ccd73ba8b16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{ lib
, fetchurl
, vscode-utils
, unzip
, patchelf
, makeWrapper
, icu
, stdenv
, openssl
, mono
}:

let
  # Get as close as possible as the `package.json` required version.
  # This is what drives omnisharp.
  rtDepsSrcsFromJson = lib.importJSON ./rt-deps-bin-srcs.json;

  rtDepsBinSrcs = builtins.mapAttrs (k: v:
      let
        # E.g: "OmniSharp-x86_64-linux"
        kSplit = builtins.split "(__)" k;
        name = builtins.elemAt kSplit 0;
        system = builtins.elemAt kSplit 2;
      in
      {
        inherit name system;
        installPath = v.installPath;
        binaries = v.binaries;
        bin-src = fetchurl {
          urls = v.urls;
          inherit (v) sha256;
        };
      }
    )
    rtDepsSrcsFromJson;

  rtDepBinSrcByName = bSrcName:
    rtDepsBinSrcs."${bSrcName}__${stdenv.targetPlatform.system}";

  omnisharp = rtDepBinSrcByName "OmniSharp";
  vsdbgs = [
    (rtDepBinSrcByName "Debugger")
  ] ++ lib.optionals (stdenv.isDarwin) [
  # Include the aarch64-darwin debugger binaries on x86_64-darwin.  Even though OmniSharp will be
  # running under Rosetta 2, debugging will fail to start if both sets of binaries are not present.
    (rtDepsBinSrcs."Debugger__aarch64-darwin")
  ];
  razor = rtDepBinSrcByName "Razor";
in

vscode-utils.buildVscodeMarketplaceExtension {
  mktplcRef = {
    name = "csharp";
    publisher = "ms-dotnettools";
    version = "1.23.16";
    sha256 = "sha256-fM4vcSMi2tEjIox9Twh2sRiFhXgAeRwAM9to3vtcSqI=";
  };

  nativeBuildInputs = [
    unzip
    patchelf
    makeWrapper
  ];

  postPatch = ''
    declare ext_unique_id
    # See below as to why we cannot take the whole basename.
    ext_unique_id="$(basename "$out" | head -c 32)"

    # Fix 'Unable to connect to debuggerEventsPipeName .. exceeds the maximum length 107.' when
    # attempting to launch a specific test in debug mode. The extension attemps to open
    # a pipe in extension dir which would fail anyway. We change to target file path
    # to a path in tmp dir with a short name based on the unique part of the nix store path.
    # This is however a brittle patch as we're working on minified code.
    # Hence the attempt to only hold on stable names.
    # However, this really would better be fixed upstream.
    sed -i \
      -E -e 's/(this\._pipePath=[a-zA-Z0-9_]+\.join\()([a-zA-Z0-9_]+\.getExtensionPath\(\)[^,]*,)/\1require("os").tmpdir(), "'"$ext_unique_id"'"\+/g' \
      "$PWD/dist/extension.js"

    unzip_to() {
      declare src_zip="''${1?}"
      declare target_dir="''${2?}"
      mkdir -p "$target_dir"
      if unzip "$src_zip" -d "$target_dir"; then
        true
      elif [[ "1" -eq "$?" ]]; then
        1>&2 echo "WARNING: unzip('$?' -> skipped files)."
      else
        1>&2 echo "ERROR: unzip('$?')."
      fi
    }

    patchelf_add_icu_as_needed() {
      declare elf="''${1?}"
      declare icu_major_v="${
        with builtins; head (splitVersion (parseDrvName icu.name).version)}"

      for icu_lib in icui18n icuuc icudata; do
        patchelf --add-needed "lib''${icu_lib}.so.$icu_major_v" "$elf"
      done
    }

    patchelf_common() {
      declare elf="''${1?}"

      patchelf_add_icu_as_needed "$elf"
      patchelf --add-needed "libssl.so" "$elf"
      patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
        --set-rpath "${lib.makeLibraryPath [ stdenv.cc.cc openssl.out icu.out ]}:\$ORIGIN" \
        "$elf"
    }

    declare omnisharp_dir="$PWD/${omnisharp.installPath}"
    unzip_to "${omnisharp.bin-src}" "$omnisharp_dir"
    rm "$omnisharp_dir/bin/mono"
    ln -s -T "${mono}/bin/mono" "$omnisharp_dir/bin/mono"
    chmod a+x "$omnisharp_dir/run"
    touch "$omnisharp_dir/install.Lock"

  '' + builtins.concatStringsSep "\n" (map (vsdbg: ''
    declare vsdbg_dir="$PWD/${vsdbg.installPath}"
    unzip_to "${vsdbg.bin-src}" "$vsdbg_dir"
    chmod a+x "$vsdbg_dir/vsdbg-ui"
    chmod a+x "$vsdbg_dir/vsdbg"
    touch "$vsdbg_dir/install.complete"
    touch "$vsdbg_dir/install.Lock"

  '') vsdbgs) + ''
    declare razor_dir="$PWD/${razor.installPath}"
    unzip_to "${razor.bin-src}" "$razor_dir"
    chmod a+x "$razor_dir/rzls"
    touch "$razor_dir/install.Lock"

  '' + lib.optionalString stdenv.isLinux ''
    patchelf_common "$vsdbg_dir/vsdbg"
    patchelf_common "$vsdbg_dir/vsdbg-ui"
    patchelf_common "$razor_dir/rzls"

  '' + lib.optionalString stdenv.isDarwin ''
    substituteInPlace $omnisharp_dir/etc/config \
      --replace "libmono-native-compat.dylib" "libmono-native.dylib"
  '';

  meta = with lib; {
    description = "C# for Visual Studio Code (powered by OmniSharp)";
    homepage = "https://github.com/OmniSharp/omnisharp-vscode";
    license = licenses.mit;
    maintainers = [ maintainers.jraygauthier ];
    platforms = [ "x86_64-linux" "x86_64-darwin" ];
  };
}