Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove broken symlinks when updating deps #2842

Merged
merged 9 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions hack/update-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ set -o pipefail

source $(dirname $0)/../vendor/github.com/knative/test-infra/scripts/library.sh

# Remove two type of invalid symlinks
# 1. Broken symlinks
# 2. Symlinks pointing outside of the source tree
# We use recursion to find the canonical path of a symlink
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this is not a recursive function :)

# because both `realpath` and `readlink -m` are not available on Mac.
function remove_invalid_symlink() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is overkill. You don't need to traverse the directory tree, only check the target. As we discussed offline, parsing ls should be simple and do the trick.

Once this is merged, definitely deserves being moved to test-infra, together with a helper for the major features of update-deps.sh.

/hold

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't quite understand that you meant (sorry for not being a shell expert). The command you reference gives a canonical path, however, for symlink even if we don't follow it all the way, we still need to follow it once by readlink then generate canonical path for it.

By over overkill, could you hint me an oneliner?

Also I agree once this is merged I will go ahead with test-infra and refactor here.

Copy link
Contributor

@adrcunha adrcunha Jan 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Remove symlinks in /vendor that are broken or lead outside the repo.
function remove_broken_symlinks() {
  for link in $(find ./vendor -type l); do
    local target="$(ls -l ${link})"
    target="${target##* -> }"
    # Remove broken symlinks
    if [[ ! -e ${target} ]]; then
      rm -f ${link}
      continue
    fi
    # Get canonical path to target, remove if outside the repo
    target="$(cd ${target%/*} && echo $PWD/${target##*/})"
    if [[ ${target} != *github.com/knative/* ]]; then
      rm -f ${link}
      continue
    fi
  done
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @adrcunha for the hint!

On the other hand however, after using your changes there are some valid symlinks got deleted, see the following log:

$ ./hack/update-deps.sh
unlinking broken:  ./vendor/github.com/knative/build/test/panic/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/test/workingdir/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/cmd/controller/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/cmd/logs/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/cmd/nop/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/cmd/git-init/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/cmd/creds-init/kodata/LICENSE
unlinking broken:  ./vendor/github.com/knative/build/cmd/webhook/kodata/LICENSE
unlinking broken:  ./vendor/github.com/prometheus/procfs/fixtures/self

However, e.g. https://github.com/knative/serving/blob/master/vendor/github.com/knative/build/test/panic/kodata/LICENSE is actually a valid symlink.

Any reason why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bug in my code, -e ${target} must be -e ${link}.

Copy link
Contributor

@adrcunha adrcunha Jan 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revised code. The real path resolver was buggy as well (I should have tested it when I wrote it the first time). The output now correctly doesn't include kodata/LICENSE.

# Remove symlinks in /vendor that are broken or lead outside the repo.
function remove_broken_symlinks() {
  for link in $(find ./vendor -type l); do
    local target="$(ls -l ${link})"
    target="${target##* -> }"
    # Remove broken symlinks
    if [[ ! -e ${link} ]]; then
      echo "[DOESN'T EXIST] rm -f ${link}"
      continue
    fi
    # Get canonical path to target, remove if outside the repo
    [[ ${target} == /* ]] || target="./${target}"
    target="$(cd `dirname ${link}` && cd ${target%/*} && echo $PWD/${target##*/})"
    if [[ ${target} != *github.com/knative/* ]]; then
      echo "[OUTSIDE TREE] rm -f ${link}"
      continue
    fi
  done
}

cd ${REPO_ROOT_DIR}

local target_file=$1
if [[ ! -e ${target_file} ]] ; then
echo "Removing broken symlink:" $1
unlink ${REPO_ROOT_DIR}/$1
return
fi

cd $(dirname ${target_file})
target_file=$(basename ${target_file})
# Iterate down a [possible] chain of symlinks
while [[ -L ${target_file} ]] ; do
target_file=$(readlink ${target_file})
cd $(dirname ${target_file})
target_file=$(basename ${target_file})
done

# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
local phys_dir=`pwd -P`
target_file=${phys_dir}/${target_file}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is producing a double slash in the output, e.g.:

Removing broken symlink: vendor//k8s.io/kubernetes/cluster/gce/cos

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I commented on the wrong line, but this line is affected too:

Removing symlink outside the source tree:  vendor//github.com/prometheus/procfs/fixtures/26231/exe -> /usr/bin/vim

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that the messages help during development, I think this is too verbose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Em... On linux the path looks right...

And as @adrcunha commented I will just delete these msg in the followup commit.


if [[ ${target_file} != *"github.com/knative/serving"* ]]; then
echo "Removing symlink outside the source tree: " $1 "->" ${target_file}
unlink ${REPO_ROOT_DIR}/$1
fi
}



cd ${REPO_ROOT_DIR}

# Ensure we have everything we need under vendor/
Expand All @@ -35,3 +72,8 @@ update_licenses third_party/VENDOR-LICENSE "./cmd/*"
# cherrypick of #66078. Remove this once that reaches a client version
# we have pulled in.
git apply ${REPO_ROOT_DIR}/hack/66078.patch

# Remove all invalid symlinks under ./vendor
for l in $(find vendor/ -type l); do
remove_invalid_symlink $l
done

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/docker/docker/project/CONTRIBUTORS.md

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/knative/build/config/300-imagecache.yaml

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/exe

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/fd/0

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/fd/1

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/fd/10

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/fd/2

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/fd/3

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/ns/mnt

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26231/ns/net

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26232/fd/0

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26232/fd/1

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26232/fd/2

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26232/fd/3

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/prometheus/procfs/fixtures/26232/fd/4

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/.bazelrc

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/.kazelcfg.json

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/BUILD.bazel

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/Makefile

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/Makefile.generated_files

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/WORKSPACE

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/cluster/gce/cos

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/cluster/gce/custom

This file was deleted.

1 change: 0 additions & 1 deletion vendor/k8s.io/kubernetes/cluster/gce/ubuntu

This file was deleted.

This file was deleted.

This file was deleted.