# Buggy code
def from_tar(b_path, force, parent=None):
    if not tarfile.is_tarfile(b_path):
        raise AnsibleError(
            "Collection artifact at '%s' is not a valid tar file." % to_native(b_path)
        )

    info = {}
    with tarfile.open(b_path, mode="r") as collection_tar:
        for b_member_name, property_name in CollectionRequirement._FILE_MAPPING:
            n_member_name = to_native(b_member_name)
            try:
                member = collection_tar.getmember(n_member_name)
            except KeyError:
                raise AnsibleError(
                    "Collection at '%s' does not contain the required file %s."
                    % (to_native(b_path), n_member_name)
                )

            with _tarfile_extract(collection_tar, member) as member_obj:
                try:
                    info[property_name] = json.loads(
                        to_text(member_obj.read(), errors="surrogate_or_strict")
                    )
                except ValueError:
                    raise AnsibleError(
                        "Collection tar file member %s does not contain a valid json string."
                        % n_member_name
                    )

    meta = info["manifest_file"]["collection_info"]
    files = info["files_file"]["files"]

    namespace = meta["namespace"]
    name = meta["name"]
    version = meta["version"]
    meta = CollectionVersionMetadata(
        namespace, name, version, None, None, meta["dependencies"]
    )

    return CollectionRequirement(
        namespace,
        name,
        b_path,
        None,
        [version],
        version,
        force,
        parent=parent,
        metadata=meta,
        files=files,
    )


# Traceback (KeyError)
Exception, this is probably a bug: "filename 'tests/unit/modules/placebo_recordings/aws_direct_connect_link_aggregation_group/nonexistent_lag_status' not found"
the full traceback was:
Traceback (most recent call last):
File "/home/bcoca/work/ansible/bin/ansible-galaxy", line 123, in <module>
exit_code = cli.run()
File "/home/bcoca/work/ansible/lib/ansible/cli/galaxy.py", line 490, in run
context.CLIARGS['func']()
File "/home/bcoca/work/ansible/lib/ansible/cli/galaxy.py", line 1057, in execute_install
self._execute_install_collection(collection_requirements, collection_path)
File "/home/bcoca/work/ansible/lib/ansible/cli/galaxy.py", line 1079, in _execute_install_collection
no_deps, force, force_with_deps, allow_pre_release=allow_pre_release)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 675, in install_collections
collection.install(output_path, b_temp_path)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 250, in install
self.install_artifact(b_collection_path, b_temp_path)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 275, in install_artifact
_extract_tar_dir(collection_tar, file_name, b_collection_path)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 1387, in _extract_tar_dir
tar_member = tar.getmember(to_native(dirname, errors='surrogate_or_strict'))
File "/usr/lib/python3.7/tarfile.py", line 1752, in getmember
raise KeyError("filename %r not found" % name)
KeyError: "filename 'tests/unit/modules/placebo_recordings/aws_direct_connect_link_aggregation_group/nonexistent_lag_status' not found"

# Fixed code
def from_tar(b_path, force, parent=None):
    if not tarfile.is_tarfile(b_path):
        raise AnsibleError(
            "Collection artifact at '%s' is not a valid tar file." % to_native(b_path)
        )

    info = {}
    with tarfile.open(b_path, mode="r") as collection_tar:
        for b_member_name, property_name in CollectionRequirement._FILE_MAPPING:
            n_member_name = to_native(b_member_name)
            try:
                member = collection_tar.getmember(n_member_name)
            except KeyError:
                raise AnsibleError(
                    "Collection at '%s' does not contain the required file %s."
                    % (to_native(b_path), n_member_name)
                )

            with _tarfile_extract(collection_tar, member) as (dummy, member_obj):
                try:
                    info[property_name] = json.loads(
                        to_text(member_obj.read(), errors="surrogate_or_strict")
                    )
                except ValueError:
                    raise AnsibleError(
                        "Collection tar file member %s does not contain a valid json string."
                        % n_member_name
                    )

    meta = info["manifest_file"]["collection_info"]
    files = info["files_file"]["files"]

    namespace = meta["namespace"]
    name = meta["name"]
    version = meta["version"]
    meta = CollectionVersionMetadata(
        namespace, name, version, None, None, meta["dependencies"]
    )

    return CollectionRequirement(
        namespace,
        name,
        b_path,
        None,
        [version],
        version,
        force,
        parent=parent,
        metadata=meta,
        files=files,
    )