Unverified Commit ff091507 authored by Gaétan Lepage's avatar Gaétan Lepage Committed by GitHub
Browse files

basedpyright: 1.19.0 -> 1.21.0 (#354204)

parents 54baabae 44992762
Loading
Loading
Loading
Loading
+0 −12120

File deleted.

Preview size limit exceeded, changes collapsed.

+40 −82
Original line number Diff line number Diff line
@@ -3,112 +3,62 @@
  fetchFromGitHub,
  runCommand,
  buildNpmPackage,
  stdenvNoCC,
  docify,
  testers,
  writeText,
  jq,
  python3,
  basedpyright,
  pkg-config,
  libsecret,
  nix-update-script,
  versionCheckHook,
}:

let
  version = "1.19.0";
buildNpmPackage rec {
  pname = "basedpyright";
  version = "1.21.0";

  src = fetchFromGitHub {
    owner = "detachhead";
    repo = "basedpyright";
    rev = "refs/tags/v${version}";
    hash = "sha256-R8QEINTlO84YrnfRsZU5vNEwYcy5uZEKmmTPJ4qxYk8=";
  };

  # To regenerate the patched package-lock.json, copy the patched package.json
  # and run `nix-shell -p nodejs --command 'npm update --package-lock'`
  patchedPackageJSON = runCommand "package.json" { } ''
    ${jq}/bin/jq '
      .devDependencies |= with_entries(select(.key == "glob" or .key == "jsonc-parser" or .key == "@detachhead/ts-helpers"))
      | .scripts =  {  }
      ' ${src}/package.json > $out
  '';

  pyright-root = buildNpmPackage {
    pname = "pyright-root";
    inherit version src;
    npmDepsHash = "sha256-vxfoaShk3ihmhr/5/2GSOuMqeo6rxebO6aiD3DybjW4=";
    dontNpmBuild = true;
    postPatch = ''
      cp ${patchedPackageJSON} ./package.json
      cp ${./package-lock.json} ./package-lock.json
    '';
    installPhase = ''
      runHook preInstall
      cp -r . "$out"
      runHook postInstall
    '';
  };

  pyright-internal = buildNpmPackage {
    pname = "pyright-internal";
    inherit version src;
    sourceRoot = "${src.name}/packages/pyright-internal";
    npmDepsHash = "sha256-Xswt1kH9Iet6DSAlZctEV3qCpn+IWwceQgldyFzsaRk=";
    dontNpmBuild = true;
    # Uncomment this flag when using unreleased peer dependencies
    # npmFlags = [ "--legacy-peer-deps" ];
    installPhase = ''
      runHook preInstall
      cp -r . "$out"
      runHook postInstall
    '';
    hash = "sha256-OQXqwpvYIitWGWshEv1/j2hAphFnGXDuvbRav5TilI4=";
  };

  docstubs = stdenvNoCC.mkDerivation {
    name = "docstubs";
    inherit src;
    nativeBuildInputs = [ docify ];
  npmDepsHash = "sha256-hCZ68sLpQs/7SYVf3pMAHfstRm1C/d80j8fESIFdhnw=";
  npmWorkspace = "packages/pyright";

    installPhase = ''
      runHook preInstall
  preBuild = ''
    # Build the docstubs
    cp -r packages/pyright-internal/typeshed-fallback docstubs
    docify docstubs/stdlib --builtins-only --in-place
      cp -rv docstubs "$out"
      runHook postInstall
  '';
  };
in
buildNpmPackage rec {
  pname = "basedpyright";
  inherit version src;

  sourceRoot = "${src.name}/packages/pyright";
  npmDepsHash = "sha256-YhXVoh5sAnUkuc4iDMd/N15y92vMhUwT7PlLn2pzb20=";
  nativeBuildInputs = [
    docify
    pkg-config
  ];

  postPatch = ''
    chmod +w ../../
    ln -s ${docstubs} ../../docstubs
    ln -s ${pyright-root}/node_modules ../../node_modules
    chmod +w ../pyright-internal
    ln -s ${pyright-internal}/node_modules ../pyright-internal/node_modules
  '';
  buildInputs = [ libsecret ];

  postInstall = ''
    mv "$out/bin/pyright" "$out/bin/basedpyright"
    mv "$out/bin/pyright-langserver" "$out/bin/basedpyright-langserver"
  '';

  dontNpmBuild = true;
  nativeInstallCheckInputs = [ versionCheckHook ];
  versionCheckProgramArg = [ "--version" ];
  doInstallCheck = true;

  passthru = {
    updateScript = ./update.sh;
    updateScript = nix-update-script { };
    tests = {
      version = testers.testVersion { package = basedpyright; };

      # We are expecting 3 errors. Any other amount would indicate, not working
      # We are expecting 4 errors. Any other amount would indicate not working
      # stub files, for instance.
      simple = testers.testEqualContents {
        assertion = "simple type checking";
        expected = writeText "expected" ''
          3
          4
        '';
        actual =
          runCommand "actual"
@@ -117,16 +67,24 @@ buildNpmPackage rec {
                jq
                basedpyright
              ];
              base = writeText "base" ''
              base = writeText "test.py" ''
                import sys
                from time import tzset

                if sys.platform == "win32":
                    a = "a" + 1
                def print_string(a_string: str):
                    a_string += 42
                    print(a_string)

                print(3)
                nonexistentfunction(3)
                if sys.platform == "win32":
                    print_string(69)
                    this_function_does_not_exist("nice!")
                else:
                    result_of_tzset_is_None: str = tzset()
              '';
              configFile = writeText "pyproject.toml" ''
                [tool.pyright]
                typeCheckingMode = "strict"
              '';

            }
            ''
              (basedpyright --outputjson $base || true) | jq -r .summary.errorCount > $out
+0 −44
Original line number Diff line number Diff line
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps
set -euo pipefail

version=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -v -s https://api.github.com/repos/detachhead/basedpyright/releases/latest | jq -r '.tag_name | sub("^v"; "")')

update-source-version basedpyright "$version"

root="$(dirname "$(readlink -f "$0")")"
FILE_PATH="$root/package.nix"
REPO_URL_PREFIX="https://github.com/detachhead/basedpyright/raw"
TEMP_DIR=$(mktemp -d)

trap 'rm -rf "$TEMP_DIR"' EXIT

# Function to download `package-lock.json` for a given source path and update hash
update_hash() {
    local source_root_path="$1"
    local existing_hash="$2"

    # Formulate download URL
    local download_url="${REPO_URL_PREFIX}/v${version}${source_root_path}/package-lock.json"

    # Download package-lock.json to temporary directory
    curl -fsSL -v -o "${TEMP_DIR}/package-lock.json" "$download_url"

    # Calculate the new hash
    local new_hash
    new_hash=$(prefetch-npm-deps "${TEMP_DIR}/package-lock.json")

    # Update npmDepsHash in the original file
    sed -i "s|$existing_hash|${new_hash}|" "$FILE_PATH"
}

while IFS= read -r source_root_line; do
    [[ "$source_root_line" =~ sourceRoot ]] || continue
    source_root_path=$(echo "$source_root_line" | sed -e 's/^.*"${src.name}\(.*\)";.*$/\1/')

    # Extract the current npmDepsHash for this sourceRoot
    existing_hash=$(grep -A1 "$source_root_line" "$FILE_PATH" | grep 'npmDepsHash' | sed -e 's/^.*npmDepsHash = "\(.*\)";$/\1/')

    # Call the function to download and update the hash
    update_hash "$source_root_path" "$existing_hash"
done < "$FILE_PATH"